leetcode 81. Search in Rotated Sorted Array II 旋转数组查找+二分查找
2017-09-11 21:59
471 查看
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 7 might 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.
这道题很简单,直接遍历即可。也可以使用二分查找。
代码如下:
下面是C++的做法,我是直接遍历
代码如下:
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 7 might 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.
这道题很简单,直接遍历即可。也可以使用二分查找。
代码如下:
public class Solution { /* * 最简单的就是遍历 * */ public boolean search(int[] nums, int target) { if(nums==null || nums.length<=0) return false; for(int i=0;i<nums.length;i++) if(nums[i]==target) return true; return false; } /* * 二分查找 * */ public boolean search2(int[] nums, int target) { if(nums==null || nums.length<=0) return false; int left=0,right=nums.length-1; while(left<=right) { int mid = (left-right)/2+right; if(nums[mid]==target) return true; if(nums[left]<nums[mid]) { if(nums[left]<=target&& target<nums[mid]) right=mid-1; else left=mid+1; }else if(nums[left]>nums[mid]) { if(nums[mid]<target&&target<=nums[right]) left=mid+1; else right=mid-1; }else left++; } return false; } }
下面是C++的做法,我是直接遍历
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; class Solution { public: bool search(vector<int>& a, int target) { if (a.size() <= 0) return false; int left = 0, right = a.size() - 1; while (left <= right) { int mid = (right - left) / 2 + left; if (a[mid] == target) return true; else if (a[left] < a[mid]) { if (a[left] <= target && target < a[mid]) right = mid - 1; else left = mid + 1; } else if (a[left] > a[mid]) { if (a[mid] < target && target <= a[right]) left = mid + 1; else right = mid - 1; } else left++; } return false; } };
相关文章推荐
- leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找
- LeetCode-Search in Rotated Sorted Array II-在旋转数组中搜索-二分查找
- Leetcode刷题记—— 81. Search in Rotated Sorted Array II(在翻转数组中查找2)
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
- leetcode_81——Search in Rotated Sorted Array II(二分查找)
- LeetCode 81. Search in Rotated Sorted Array II(搜索旋转的数组)
- leetcode 刷题之路 35 Search in Rotated Sorted Array II (旋转排序数组查找终极版)
- leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
- Search in Rotated Sorted Array II 旋转数组二分查找 有重复
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
- LeetCode @ Search in Rotated Sorted ArrayII 旋转数组查找II
- LeetCode-Find Minimum in Rotated Sorted Array II-旋转排序数组找最小-二分查找
- Leetcode 81. Search in Rotated Sorted Array II 旋转数组查询2 解题报告
- LeetCode------33. Search in Rotated Sorted Array(旋转数组中查找)
- [LeetCode]26. Search in Rotated Array II旋转数组查找II
- Leetcode--Search in Rotated Sorted Array(旋转数组的查找)
- LeetCode @ Search in Rotated Sorted Array 旋转数组查找
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
- LeetCode | Search in Rotated Sorted Array(在旋转数组中查找)