Last updated 6 years ago
Was this helpful?
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed?Would this affect the run-time complexity? How and why?
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
不能再用binary search了,因为pivot elements可以重复后start 和end都可以等于pivot, 无法再判断mid是在前段还是后段。
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; } }
O(n)