LeetCode - 33/81/153/154 - Search in Rotated Sorted Array
2017-07-26 18:37
501 查看
33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order 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.
在一个旋转过的数组中(数字不重复)查找一个数。
就,分情况讨论。主要是脑子得清醒,不然很容易晕orz。时间复杂度O(logn)
81. Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,
Write a function to determine if a given target is in the array.
The array may contain duplicates.
在一个旋转过的数组中(数字有重复)查找一个数。
嗯,整体思路不变,只要要去除重复的数。时间复杂度O(n)感觉最坏情况下确实有这么多。。。
153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,
Find the minimum element.
You may assume no duplicate exists in the array.
在一个旋转过的数组中寻找最小值。时间复杂度O(logn)
154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,
Find the minimum element.
The array may contain duplicates.
在一个旋转过的数组中找到最小值(数组有重复值)。时间复杂度O(n)
Suppose an array sorted in ascending order 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.
在一个旋转过的数组中(数字不重复)查找一个数。
就,分情况讨论。主要是脑子得清醒,不然很容易晕orz。时间复杂度O(logn)
class Solution { public: int search(vector<int>& nums, int target) { if (nums.empty()) return -1; int le = 0, ri = nums.size() - 1; while (le < ri) { int mid = (le + ri) >> 1; if (nums[mid] == target) return mid; else if (nums[le] <= nums[mid]) { if (target >= nums[le] && target < nums[mid]) ri = mid - 1; else le = mid + 1; } else { if (target > nums[mid] && target <= nums[ri]) le = mid + 1; else ri = mid - 1; } } return nums[le] == target ? le : -1; } };
81. Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order 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).
Write a function to determine if a given target is in the array.
The array may contain duplicates.
在一个旋转过的数组中(数字有重复)查找一个数。
嗯,整体思路不变,只要要去除重复的数。时间复杂度O(n)感觉最坏情况下确实有这么多。。。
class Solution { public: bool search(vector<int>& nums, int target) { if (nums.empty()) return false; int le = 0, ri = nums.size() - 1; while (le < ri) { while (le < ri && nums[le] == nums[le+1]) le++; while (le < ri && nums[ri] == nums[ri-1]) ri--; if (le >= ri) break; int mid = (le + ri) >> 1; if (nums[mid] == target) return true; if (nums[le] <= nums[mid]) { if (target >= nums[le] && target < nums[mid]) ri = mid - 1; else le = mid + 1; } else { if (target > nums[mid] && target <= nums[ri]) le = mid + 1; else ri = mid - 1; } } return nums[le] == target ? true : false; } };
153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order 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).
Find the minimum element.
You may assume no duplicate exists in the array.
在一个旋转过的数组中寻找最小值。时间复杂度O(logn)
class Solution { public: int findMin(vector<int>& nums) { if (nums.empty()) return -1; int mmin = nums[0]; int le = 0, ri = nums.size() - 1; while (le < ri) { int mid = (le + ri) >> 1; if (nums[ri] >= nums[mid]) ri = mid; else if (nums[le] <= nums[mid]) le = mid + 1; } return nums[le]; } };
154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order 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).
Find the minimum element.
The array may contain duplicates.
在一个旋转过的数组中找到最小值(数组有重复值)。时间复杂度O(n)
class Solution { public: int findMin(vector<int>& nums) { if (nums.empty()) return -1; int le = 0, ri = nums.size() - 1; while (le < ri) { while (le < ri && nums[le] == nums[le+1]) le++; while (le < ri && nums[ri] == nums[ri-1]) ri--; if (le >= ri) break; int mid = (le + ri) >> 1; if (nums[mid] <= nums[ri]) ri = mid; else if (nums[mid] >= nums[le]) le = mid + 1; } return nums[le]; } };
相关文章推荐
- leetcode题解-33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
- LeetCode(33)(81) Search in Rotated Sorted Array I II
- Leetcode 33, 81 Search in Rotated Sorted Array I, II
- leetcode 33|81. Search in Rotated Sorted Array 1|2
- LeetCode 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
- LeetCode 33,81. Search in Rotated Sorted Array i, ii
- [Leetcode]33(81). Search in Rotated Sorted Array I, II
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
- leetcode 81. Search in Rotated Sorted Array II 旋转数组查找+二分查找
- Java [leetcode 33]Search in Rotated Sorted Array
- [LeetCode]33. Search in Rotated Sorted Array
- leetcode - 33.Search in Rotated Sorted Array
- leetcode 81. Search in Rotated Sorted Array II
- LeetCode 33. Search in Rotated Sorted Array
- Leetcode 33. Search in Rotated Sorted Array
- 【leetcode】Array——Search in Rotated Sorted Array(33)
- leetcode 33. Search in Rotated Sorted Array
- LeetCode 31.Next Permutation & 33.Search in Rotated Sorted Array
- LeetCode 33. Search in Rotated Sorted Array
- leetcode 33. Search in Rotated Sorted Array 二分查找