Question | Answer |
Search rotated array | class Solution { public int search(int[] nums, int target) { int low = 0, high = nums.length - 1; int x = target; while(low <= high){ int mid = (low+high)/2 ; if(nums[mid] == x ) return mid; if( nums[mid] <= nums[high]){ if(x > nums[mid] && x <= nums[high]){ low = mid + 1; }else high = mid - 1; }else if(nums[mid] >= nums[low]){ if(x >= nums[low] && x < nums[mid] ){ high = mid - 1; }else low = mid + 1; } } return -1; } } |
There are no comments, be the first and leave one below:
Want to create your own Flashcards for free with GoConqr? Learn more.