/*
* 33.Search in sorted Array
* 2016-4-19 by Mingyang
* 我自己写的代码,开始没有考虑[3,1]取1得情况,所以现在需要额外的加一个部分来
* 判断只有2个数的时候
*/
public static int search1(int[] nums, int target) {
int len = nums.length;
if (len == 0 || nums == null)
return -1;
return searchHelper(nums, target, 0, len - 1);
}
public static int searchHelper(int[] nums, int target, int start, int end) {
if (start > end)
return -1;
int mid = (start + end) / 2;
//这就是多加的部分
if(mid==start||mid==end){
if(nums[start]==target)
return start;
if(nums[end]==target)
return end;
return -1;
}
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > nums[start]) {
if (target >= nums[start] && target <= nums[mid]) {
return searchHelper(nums, target, start, mid - 1);
} else {
return searchHelper(nums, target, mid + 1, end);
}
} else {
if (target >= nums[mid] && target <= nums[end]) {
return searchHelper(nums, target, mid + 1, end);
} else {
return searchHelper(nums, target, start, mid - 1);
}
}
}
/*
* 下面是网上的代码,一样的复杂度,可能更具有延展性
* 如果target比A[mid]值要小------------------------------
* 如果A[mid]右边有序(A[mid]<A[high]) 那么target肯定不在右边(target比右边的都得小),在左边找
* 如果A[mid]左边有序,那么比较target和A[low],如果target比A[low]还要小,
*证明target不在这一区,去右边找;反之,左边找。
* 如果target比A[mid]值要大-------------------------------
* 如果A[mid]左边有序(A[mid]>A[low])
* 那么target肯定不在左边(target比左边的都得大),在右边找 如果A[mid]右边有序
* 那么比较target和A[high],如果target比A[high]还要大,证明target不在这一区,去左边找;反之,右边找。
*/
public int search(int[] A, int target) {
if (A == null || A.length == 0)
return -1;
int low = 0;
int high = A.length - 1;
while (low <= high) { //这里是小于等于哦!!!!!!!!!!!!!----?
int mid = (low + high) / 2;
if (target < A[mid]) {
if (A[mid] < A[high])// right side is sorted
high = mid - 1;// target must in left side
else
if (target < A[low])
// target<A[mid]&&target<A[low]==>means,target cannot be in [low,mid] since this side is sorted
low = mid + 1;
else
high = mid - 1;
} else if (target > A[mid]) {
if (A[low] < A[mid])// left side is sorted
low = mid + 1;// target must in right side
else
if (target > A[high])
// right side is sorted. If target>A[high] means target is not in this side
high = mid - 1;
else
low = mid + 1;
} else
return mid;
}
return -1;
}

33.Search in sorted Array的更多相关文章

  1. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  2. [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路

    33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...

  3. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  4. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  5. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  6. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  7. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  8. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  9. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. shell脚本调试打印日志问题

    shell脚本调试打印日志问题 1. 需求 我们在编写脚本的时候,有时候需要做调试,便于我们定位问题,有时候等脚本上线之后,我们需要保留脚本执行过程中的记录.便于我们在出问题的时候,定位问题. 2. ...

  2. OpenCV2.4.11+VS2012的环境配置+“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    本来OpenCV环境配置的问题是个基础问题,但是步骤有点小烦,所以几乎每次都要百度一下,加上这次遇到的“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突 ...

  3. MySQLfailover错误一则

    由于公司现有主库要转移到新的主库上,所以,我打算利用MySQLfailover工具的故障转移. 1.开发把程序账号转移到新主库上 2.停止现有主库,使之进行故障转移,转移期间会自动锁表,保持数据一致性 ...

  4. Ubuntu 下的aegisub安装

    大家用开源的软件用习惯了.推荐大家使用一下字幕编辑软件 分享的是Ubuntu下的安装教程: $ sudo add-apt-repository ppa:djcj/aegisub $ sudo apt- ...

  5. 计算机/ARM 系统

    这方面知识,自己有了大概了解,可以再没有重要急需学习的安排时,不时的翻翻这方面的书.本能力属于重要但不急需的能力

  6. python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。

    def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...

  7. Solr5.0.0 DIH之增量索引

    定时索引相关知识 增量更新需要配置个sql(deltaImportQuery.deltaQuery) deltaImportQuery="select * where id='${dih.d ...

  8. solr 日志配置

    配置Solr日志记录 临时记录设置 您可以使用Admin Web界面来控制Solr中的日志输出量.选择LOGGING链接.请注意,此页面只允许您更改正在运行的系统中的设置,并不会保存在下一次​​运行中 ...

  9. 杭电 1051 Wooden Sticks

    Description There is a pile of n wooden sticks. The length and weight of each stick are known in adv ...

  10. BFS:UVa201-Squares

    Squares A children's board game consists of a square array of dots that contains lines connecting so ...