33. Search in Rotated Sorted Array
2016-07-26 19:21
302 查看
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might 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.
Subscribe to see which companies asked this question
Explanation
Let’s say nums looks like this: [12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Because it’s not fully sorted, we can’t do normal binary search. But here comes the trick:
If target is let’s say 14, then we adjust nums to this, where “inf” means infinity:
[12, 13, 14, 15, 16, 17, 18, 19, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf]
If target is let’s say 7, then we adjust nums to this:
[-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
And then we can simply do ordinary binary search.
Of course we don’t actually adjust the whole array but >instead adjust only on the fly only the elements we look >at. And the adjustment is done by comparing both the >target and the actual element against nums[0].
(i.e., 0 1 2 4 5 6 7 might 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.
Subscribe to see which companies asked this question
Explanation
Let’s say nums looks like this: [12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Because it’s not fully sorted, we can’t do normal binary search. But here comes the trick:
If target is let’s say 14, then we adjust nums to this, where “inf” means infinity:
[12, 13, 14, 15, 16, 17, 18, 19, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf]
If target is let’s say 7, then we adjust nums to this:
[-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
And then we can simply do ordinary binary search.
Of course we don’t actually adjust the whole array but >instead adjust only on the fly only the elements we look >at. And the adjustment is done by comparing both the >target and the actual element against nums[0].
int search(vector<int>& nums, int target) { int lo = 0, hi = nums.size(); while (lo < hi) { int mid = (lo + hi) / 2; double num = (nums[mid] < nums[0]) == (target < nums[0]) ? nums[mid] : target < nums[0] ? -INFINITY : INFINITY; if (num < target) lo = mid + 1; else if (num > target) hi = mid; else return mid; } return -1; }
相关文章推荐
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- 处理使用MariaDB做级联复制遇到的error 1666
- iOS上传二进制流文件,PHP接收并生成文件
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- 【leetcode】102. Binary Tree Level Order Traversal
- mysql 字符串区分大小写
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- Binary Tree Zigzag Level Order Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- Flatten Binary Tree to Linked List
- Populating Next Right Pointers in Each Node II
- Gray Code
- Add Binary @ LeetCode java
- 005-LAMP_MySQL binary installation
- Mysql查询大小写不敏感简便解决办法
- how to access and operate a binarry file ?