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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  8. 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 ...

  9. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

随机推荐

  1. windows网络编程的一些理论

    参考自<VC++深入详解> 这是我在看书时记录下来的东西. 注:下面的Socket其实都应该是socket 第14章网络编程 Socket是连接应用程序与网络驱动程序的桥梁,Socket在 ...

  2. Jquery.validate.js表单验证插件的使用

    作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例 ...

  3. linux oracle磁盘满了

    最近,查看我们一台linux服务器,发现硬盘空间都已经使用了95%,很是疑惑啊,怎么回事那?难道是数据库文件太大了? Filesystem            Size  Used Avail Us ...

  4. 详解在visual studio中使用git版本系统(图文)

    很多人已经在使用git(或正在转移到git上),在github.com上,也看到园子里不少同学的开源项目,非常不错.但相关教程似乎不多,所以趁着我自己的开源项目源码托管(https://github. ...

  5. SPOJ QTREE 树链剖分

    树链剖分的第一题,易懂,注意这里是边. #include<queue> #include<stack> #include<cmath> #include<cs ...

  6. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  7. web前端开发常用的10个高端CSS UI开源框架

    web前端开发常用的10个高端CSS UI开源框架   随着人们对体验的极致追求,web页面设计也面临着新的挑战,不仅需要更人性化的设计理念,还需要设计出更酷炫的页面.作为web前端开发人员,运用开源 ...

  8. 【kAriOJ】离散数学春季学期编程测试 1

    A.凯撒密码 题意: 给你k1,k2,和一串明文,一串密文. 明文用k1加密,密文用k2解密. 对于明文要把字母转换成大写字母,非字母全部删除. 额:要考虑到取模可能会变成负数,所以要加一下26再取模 ...

  9. 【uoj222】 NOI2016—区间

    http://uoj.ac/problem/222 (题目链接) 题意 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. Solution 线段树+滑动窗口.这道题很 ...

  10. [NOIP2011] 普及组

    数字反转 小模拟 #include<cstdio> #include<iostream> #include<cstring> using namespace std ...