*Search in Rotated Sorted Array
2015-07-10 16:01
441 查看
题目:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
题解:
这道题是一道常见的二分查找法的变体题。
要解决这道题,需要明确rotated sorted array的特性,那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下是思路:
Take “4 5 6 7 0 1 2″ as an example. The mid entry is 7. We can compare it with the first entry. If the first entry is smaller than the mid entry, then the first half (from 4 to 7) must be in strictly increasing order. So we can compare target with the first entry and the mid entry, then we can decide if the target is in this half or not. If the first entry is larger than the mid entry, then the second half (fron 7 to 2) is in strictly increasing order. We can compare the target with them. Using this algorithm, every time we can throw half of the array.
Complexity
The complexity is O(log n), which is similar to binary search.
Solution from leetcode.com.
For details: http://leetcode.com/2010/04/searching-element-in-rotated-array.html.
九章算法的模板解法:
Reference:
http://www.lifeincode.net/programming/leetcode-search-in-rotated-sorted-array-java/ http://www.cnblogs.com/springfor/p/3858140.html
http://leetcode.com/2010/04/searching-element-in-rotated-array.html.
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7might become
4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
题解:
这道题是一道常见的二分查找法的变体题。
要解决这道题,需要明确rotated sorted array的特性,那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下是思路:
Take “4 5 6 7 0 1 2″ as an example. The mid entry is 7. We can compare it with the first entry. If the first entry is smaller than the mid entry, then the first half (from 4 to 7) must be in strictly increasing order. So we can compare target with the first entry and the mid entry, then we can decide if the target is in this half or not. If the first entry is larger than the mid entry, then the second half (fron 7 to 2) is in strictly increasing order. We can compare the target with them. Using this algorithm, every time we can throw half of the array.
public class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length-1; while(left<=right) { int mid = left + (right-left)/2; if(nums[mid]==target) return mid; if(nums[mid]<nums[right]) //half right is sorted { if(target > nums[mid]&&target <= nums[right]) { left = mid+1; } else { right = mid-1; } } else //half left is sorted { if(target >= nums[left] && target< nums[mid]) { right = mid-1; } else { left = mid+1; } } } return -1; } }
Complexity
The complexity is O(log n), which is similar to binary search.
Follow up
Find the rotation pivot.Solution from leetcode.com.
int FindSortedArrayRotation(int A[], int N) { int L = 0; int R = N - 1; while (A[L] > A[R]) { int M = L + (R - L) / 2; if (A[M] > A[R]) L = M + 1; else R = M; } return L; }
For details: http://leetcode.com/2010/04/searching-element-in-rotated-array.html.
九章算法的模板解法:
/** * 本代码由九章算法编辑提供。没有版权欢迎转发。 * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。 * - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班 * - 更多详情请见官方网站:http://www.jiuzhang.com/ */ public class Solution { public int search(int[] A, int target) { if (A == null || A.length == 0) { return -1; } int start = 0; int end = A.length - 1; int mid; while (start + 1 < end) { mid = start + (end - start) / 2; if (A[mid] == target) { return mid; } if (A[start] < A[mid]) { // situation 1, red line if (A[start] <= target && target <= A[mid]) { end = mid; } else { start = mid; } } else { // situation 2, green line if (A[mid] <= target && target <= A[end]) { start = mid; } else { end = mid; } } } // while if (A[start] == target) { return start; } if (A[end] == target) { return end; } return -1; } }
Reference:
http://www.lifeincode.net/programming/leetcode-search-in-rotated-sorted-array-java/ http://www.cnblogs.com/springfor/p/3858140.html
http://leetcode.com/2010/04/searching-element-in-rotated-array.html.
相关文章推荐
- UDP指定客户端发送端口
- iOS开发-UITableView-2
- 关于5.0的主题对hint颜色值的影响
- Coded UI Test 同时执行多个测试用例不必每次都关闭浏览器
- Oracle创建类似mysql AUTO_INCREMENT自增列
- 类似百度文库在线预览文件内容技术探讨
- C++对const的用法笔记
- Dialog 设置背景完全透明的样式
- 4:Median of Two Sorted Arrays
- [leedcode 40] Combination Sum II
- Redis监控方案
- Workflow Recovery
- 黑马程序员——OC基础---面向对象(思想,类,对象,三大特性)
- Csharp调用微软COM转换ppt为HTML
- Android实现圆角边框
- 查找页面错误(两)
- redislive
- tomcat配置调优与安全总结
- Csharp调用微软COM转换excel为HTML
- centos 6.6编译安装mysql 5.6