search in 2d matrix and serach minimum in rotated array
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
class test
{
public static void main (String[] args) throws java.lang.Exception
{
int[] B = {6,7,8,9,1,2,3,4,5};
int[][] c = {{1, 3, 5, 7},{10, 11, 16, 20}, {23, 30, 34, 50}}; System.out.println(search_mini(B));
System.out.println(search(B,1));
System.out.println(Arrays.toString(search_2d_matrix(c,30)));
} public static int[] search_2d_matrix(int[][] matrix, int target){
int row = matrix.length, column = matrix[0].length;
int begin = 0, end = row * column ;
int[] result = {-1, -1}; while (begin + 1 < end) {
int mid = (begin + end) / 2;
int number = matrix[mid / column][mid % column];
if (number == target) {
result[0] = mid / column;
result[1] = mid % column;
return result;
} else if (number < target) {
begin = mid;
} else {
end = mid;
}
}
return result;
} public static int search_mini(int[] A){
int begin = 0, end = A.length - 1;
while (begin < end && A[begin] >= A[end]) {
int mid = (begin + end) / 2;
if (A[mid] > A[end]) {
begin = mid + 1;
} else if (A[mid] < A[begin]) {
end = mid;
} else { // A[begin] == A[mid] == A[end]
begin = begin + 1;
}
}
return A[begin];
} public static int search(int[] A, int target){
int begin = 0;
int end = A.length ;
while(begin < end){
int p = (end + begin) / 2;
if(A[p] == target) return p;
else if(A[p] > A[begin] ){
if(target >= A[begin] && target < A[p]){
end = p;
}
else begin = p + 1;
}
else{
if(target > A[p] && target <= A[end - 1] ){
begin = p + 1;
}
else end = p;
} }
return -1;
}
}
search in 2d matrix and serach minimum in rotated array的更多相关文章
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
随机推荐
- javascript去掉字符串前后空格
使用场景 当我们进行一些页面编辑时,字符串前后的空格,通常是无效的.因此需要在获取信息时,进行过滤. 比如: 输入:[空格][空格]a[空格]b[空格][空格][空格] 得到:a[空格]b 代码如下: ...
- HDU 5976 Detachment 打表找规律
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5976 Detachment Time Limit: 4000/2000 MS (Java/Other ...
- java并发库--锁
synchronized的缺陷: 被synchronized修饰了,当一个线程获取了对应的锁,并执行该代码块时,其他线程便只能一直等待,等待获取锁的线程释放锁,获取线程被阻塞时,没有释放锁会导致等待线 ...
- Jquery-处理iframe的高度自适应
超级简单的方法,也不用写什么判断浏览器高度.宽度啥的.下面的两种方法自选其一就行了.一个是放在和iframe同页面的,一个是放在test.html页面的.注意别放错地方了哦. iframe代码,注意要 ...
- 模式匹配KMP算法
关于KMP算法的原理网上有很详细的解释,我试着总结理解一下: KMP算法是什么 以这张图片为例子 匹配到j=5时失效了,BF算法里我们会使i=1,j=0,再看s的第i位开始能不能匹配,而KMP算法接下 ...
- CSS文字排版
一.font-size 我来试一试:为第一段中的“胆小如鼠”设置字号为:20px,字体颜色为:red. <!DOCTYPE HTML> <html> <head> ...
- .net Int16 、(int Int32)、 Int64 的区别
关于什么是16位整数,32位整数,64位整数,请看这里:http://www.cnblogs.com/EasonJim/p/4837061.html Int16 值类型表示值介于 -32768 到 + ...
- 取出list的数组元素
java中将list中的一维数组中的元素取出需要2步.第一步:获取list的迭代器,将数组从迭代器中遍历取出:第二部:对取出的数组进行遍历,取出数组中存储的元素.java的list集合中只能存储引用型 ...
- urllib2.URLError: <urlopen error [Errno 10061] >
今天来运行以前的python脚本,结果报这个错:urllib2.URLError: <urlopen error [Errno 10061] > 原来是因为 解决方法:打开IE浏览器,依次 ...
- Linux的学习路线图
一.学习Linux的基本要求1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件包的常用安装方法 4. 学习添加外 ...