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. 使用DOM动态创建标签

    本文是参考<javascript Dom 编程艺术>第八章的内容所写,用到的知识点,就是关于创建平稳的web页面. 使用DOM方法: getElementById() getElement ...

  2. Linq表达式开窍

    static IQueryable<T> GetPageList<T,TKey>(Expression<Func<T,bool>> whereLambd ...

  3. CSS重点巩固

    一:position定位 a: static 定位 ,HTML元素的默认值,即没有定位,元素出现在正常的流中.静态定位的元素不会受到top, bottom, left, right影响. b: fix ...

  4. usefull-url

    http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...

  5. 大数相乘算法C++版

    #include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...

  6. note.js之 Nodejs+Express4在windows下的配置

    本篇主要介绍一下在windows平台下采用nodejs+express4框架+Mongodb实现网站的开发.其实本人是不赞同在Windows平台下使用node.js进行开发,但由于公司后台工程师都是采 ...

  7. BIEE连接数据库的方法

    BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...

  8. org.springframework.beans.factory.annotation.Autowired(required=true)

    Injection of autowired dependencies failed ERROR org.springframework.web.context.ContextLoader  - Co ...

  9. junit加载

    Run as junit 不会出现,把junit 包倒入lib文件夹中,在类的后面加上extends TestCase,此时上方会导入一个包:import junit.framework.TestCa ...

  10. Android:View中的performClick()触发条件

    http://blog.sina.com.cn/s/blog_70ae1d7b0102v7uk.html 先看看performClick()源码:   public boolean performCl ...