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. 设置 git/npm/bower/pip/gem镜像或代理

    git 设置: $ git config --global http.proxy http://proxy.mysite.com 取消: $ git config --global --unset h ...

  2. HTC Vive 体验的折腾经历

    HTC Vive 是个什么东西, 想必我就不用介绍了, 不知道自己百度吧 HTC Vive发布已经有一段时间了, 一直很纠结买还是不买, 这玩意太贵(官网6888),买了还不能直接用, 还要配太高性能 ...

  3. Redis——分布式简单使用

    Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis安装:参考博客http://www ...

  4. Symfony学习--原创。。。。

    {{ constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION') }} //显示当前symfony的版本 #div { opacity: 0 ...

  5. 【转】document.documentElement和document.body的区别

    转自:http://www.cnblogs.com/ckmouse/archive/2012/01/30/2332070.html 网页中获取滚动条卷去部分的高度,可以通过 document.body ...

  6. javascript-XMLHttpRequest

    JS方法: var xmlhttp;//一定注意是写在外面的全局变量,我调了一个上午才发现. function verify(){ //使用dom方式获取文本框中的值 var userName=doc ...

  7. 【BZOJ】3527: [Zjoi2014]力(fft+卷积)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3527 好好的一道模板题,我自己被自己坑了好久.. 首先题目看错.......什么玩意.......首 ...

  8. 【CodeForces 616D】Longest k-Good Segment

    题意 n个数里,找到最长的一个连续序列使里面最多k个不同的数. 分析 尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直 ...

  9. 【UVA 1583】Digit Generator

    题 题意 a加上 a的各位数=b,则b是a的digitSum,a是b的generator,现在给你digitSum,让你求它的最小的generator. 分析 一种方法是: 预处理打表,也就是把1到1 ...

  10. SpringMVC配置

    博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 4  文章- 1  评论- 0  搭建springmvc框架的另一种思路 在一个完整的项目里搭建springmvc框架的时候, 通常情况下,初学者 ...