Search in Rotated Sorted Array II
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Tips
不能再用binary search了,因为pivot elements可以重复后start 和end都可以等于pivot, 无法再判断mid是在前段还是后段。
Code
class Solution {
public boolean search(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return true;
}
}
return false;
}
}
Analysis
O(n)
Last updated
Was this helpful?