搜索旋转排序数组
来源:互联网 发布:手机淘宝无法识别图片 编辑:程序博客网 时间:2024/06/08 08:03
假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。
你可以假设数组中不存在重复的元素。
分析:特殊的数组搜索某一元素问题,二分法。这一题的难点在于如何寻找二分的判断条件,可以结合中学数学思想,进行分段讨论。以数组首元素进行分段,然后每一段可以再进行一次讨论。二分的二分问题。
public int search(int[] A, int target) { if (A == null || A.length == 0) { return -1; } int start = 0; int end = A.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2; if (A[start] < A[mid]) { if (A[mid] >= target && target >= A[start]) { end = mid; } else{ start = mid; } } else { if (A[mid] <= target && A[end] >= target) { start = mid; } else { end = mid; } } } if (A[start] == target) { return start; } if (A[end] == target) { return end; } return -1; }
0 0
- 搜索旋转排序数组
- 搜索旋转排序数组
- 搜索旋转排序数组
- 搜索旋转排序数组
- 搜索旋转排序数组
- 搜索旋转排序数组
- LintCode-搜索旋转排序数组
- 搜索旋转排序数组 II
- LintCode : 搜索旋转排序数组
- LintCode:搜索旋转排序数组
- LintCode:搜索旋转排序数组
- 搜索旋转排序数组 II
- 搜索旋转排序数组 II
- *[Lintcode]搜索旋转排序数组
- lintcode,搜索旋转排序数组
- Lintcode 搜索旋转排序数组
- 搜索旋转排序数组 II
- 搜索旋转排序数组I
- CentOS6 配置FTP服务器
- 一份 c++的DoxyGen 的注释模板
- 72. Edit Distance
- 最容易理解的对卷积(convolution)的解释
- Selenium参数化-使用eclipse Arguments
- 搜索旋转排序数组
- LeetCode 111. minimum depth of binary tree
- 1056. 组合数的和(15)
- java synchronized特性
- Note7燃损真相大白 电池是祸根
- redis持久化原理详解
- pandas画图
- java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例
- box-sizing:border-box;的一些坑