[Leetcode] Search in Rotated Sorted Array II
2016-09-22 14:38
246 查看
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
public class Solution { public boolean search(int[] nums, int target) { int start = 0; int end = nums.length - 1; while(start <= end) { int mid = (start + end) / 2; if(nums[mid] == target) { return true; } else if(nums[start] < nums[mid]) { if(target > nums[mid]) { start = mid +1; } else if(target < nums[mid] && target >= nums[start]) { end = mid -1; } else if(target < nums[mid] && target < nums[start]){ start = mid + 1; } } else if(nums[start] > nums[mid]) { if(target < nums[mid]) { end = mid - 1; } else if(target > nums[mid] && target > nums[end]) { end = mid - 1; } else if(target > nums[mid] && target <= nums[end]) { start = mid + 1; } } // when nums[start] == nums[mid], it can either be because pivot is on the right of mid or because of duplicates else { start++; } } return false; } }
相关文章推荐
- [LeetCode] Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- leetcode_Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II [LeetCode]
- LeetCode-Search in Rotated Sorted Array II
- leetcode 98: Search in Rotated Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II
- 【leetcode】Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II 解题报告
- [Leetcode] Search in Rotated Sorted Array II
- LeetCode-Search in Rotated Sorted Array II
- leetcode Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array II
- LeetCode - Search in Rotated Sorted Array II
- LeetCode : Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- leetcode - Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- [LeetCode]Search in Rotated Sorted Array II