1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Solution { public boolean search(int[] nums, int target) { int left = 0; int right = nums.length-1; while (left <= right){ int mid = (left+right)/2; if (nums[mid] == target) return true; if (nums[mid] < nums[right]){ if(target > nums[mid] && target <= nums[right]){ left = mid + 1; }else{ right = mid-1; } }else if (nums[mid] > nums[right]){ if (target < nums[mid] && target >= nums[left]){ right = mid-1; }else{ left = mid+1; } }else{ right--; } } return false; } } |
Saturday, November 21, 2015
[leetcode]Search in rotated sorted array II
关键就是肯定有一边是sorted的...所以只要看最右边究竟是 > , <还是== 就好了.....
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment