081.Search in Rotated Sorted Array II Posted on 2017-09-16 | In LeetCode Test cases1234567891011121314[1,3,1,1,1]3[1,1,3,1]3[1,1,1, 3,1]3[]5[1,1,1,1,1]1[1,1,1,2,2,2]1[1,1,1,2,2,2]2 Solution 1: accepted 1msHow to modularize the problem: Time: O(n) // worst case, when all elements are the same 1234567891011121314151617181920212223242526public boolean search(int[] nums, int target) { if (nums.length == 0) return false; int lo = 0; int hi = nums.length - 1; while (lo + 1 < hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] == target) return true; if (nums[mid] > nums[hi]) { if (target >= nums[lo] && target < nums[mid]) hi = mid; else lo = mid; } else if (nums[mid] < nums[hi]) { if (target <= nums[hi] && target > nums[mid]) lo = mid; else hi = mid; } else { hi--; } } if (nums[lo] == target || nums[hi] == target) return true; return false;}