leetcode 81. 搜索旋转排序数组 II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。
编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。
示例 1:
输入: nums = [2,5,6,0,0,1,2], target = 0
输出: true
示例 2:
输入: nums = [2,5,6,0,0,1,2], target = 3
输出: false
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
1 public class _81 { 2 public boolean search(int[] nums, int target) { 3 if (nums.length == 2) return nums[0] == target || nums[1] == target; 4 if (nums.length == 1) return nums[0] == target; 5 if (nums.length == 0) return false; 6 int low = 0, high = nums.length-1, mid = (low+high)/2; 7 int nsl = 0, nsr = high; 8 9 if (nums[low] == nums[mid] && nums[mid] == nums[high]) { 10 for (int i = 0; i < nums.length; i++) 11 if (nums[i] == target) 12 return true; 13 return false; 14 } 15 16 // 找出有序的那部分 17 int[] t = null; 18 if (nums[mid] <= nums[high]){ // 左边旋转过,右边有序 19 t = Arrays.copyOfRange(nums, mid, high+1); 20 nsl = low; 21 nsr = mid+1; 22 } else { 23 t = Arrays.copyOfRange(nums, low, mid+1); 24 nsl = mid; 25 nsr = high+1; 26 } 27 int res = Arrays.binarySearch(t, target); 28 if (res >= 0) return true; 29 30 // 判断无序的左边是否存在target 31 return search(Arrays.copyOfRange(nums, nsl, nsr), target); 32 } 33 34 public static void main(String[] args) { 35 int[] nums = {1,1,1,1,1,1,1,2,1,1}; 36 boolean res = new _81().search(nums, 2); 37 System.out.println("res = " + res); 38 } 39 }
稍微改进下。
1 public class _81 { 2 public boolean binarySearch(int[] nums, int low, int high, int target){ // 包括high位置 3 int mid; 4 5 while(low <= high){ 6 mid = (low+high)/2; 7 if (nums[mid] < target){ 8 low = mid+1; 9 } else if (nums[mid] > target){ 10 high = mid - 1; 11 } else { 12 return true; 13 } 14 } 15 return false; 16 } 17 public boolean result(int[] nums, int target, int low, int high){ 18 if (low+1==high) return nums[low] == target || nums[high] == target; 19 if (low == high) return nums[low] == target; 20 int mid = (low+high)/2; 21 22 if (nums[low] == nums[mid] && nums[mid] == nums[high]) { 23 for (int i = low; i <= high; i++) 24 if (nums[i] == target) 25 return true; 26 return false; 27 } 28 29 if (nums[mid] <= nums[high]){ // 右边有序,且taregt存在于右边 30 if (target <= nums[high] && target >= nums[mid]) 31 return binarySearch(nums, mid, high, target); 32 // 查找无序的左边 33 return result(nums, target, low, mid); 34 } 35 if (nums[low] <= nums[mid]){ // 左边有序,且target存在于左边 36 if (target <= nums[mid] && target >= nums[low]) 37 return binarySearch(nums, low, mid, target); 38 // 查找无序的右边 39 return result(nums, target, mid, high); 40 } 41 return false; 42 } 43 public boolean search(int[] nums, int target) { 44 if (nums.length == 2) return nums[0] == target || nums[1] == target; 45 if (nums.length == 1) return nums[0] == target; 46 if (nums.length == 0) return false; 47 return result(nums, target, 0, nums.length-1); 48 } 49 50 public static void main(String[] args) { 51 int[] nums = {2,5,6,0,0,1,2}; 52 boolean res = new _81().search(nums, 8); 53 System.out.println("res = " + res); 54 } 55 }