给定一个非降序的数组 ,对第 位之后的部分进行旋转(将 及以后的部分按原数组顺序移到前面),即 中的元素满足 。
例如,对于数组 ,取 进行旋转,那么得到的数组是
特殊的 , 若 ,则原数组不发生任何改变。
现在给定一个数 ,请你在数组 中搜索 是否存在。若存在则返回 true ,否则返回 false 。
要求空间复杂度 ,时间复杂度
[1],1
true
import java.util.*; public class Solution { /** * * @param A int整型一维数组 * @param target int整型 * @return bool布尔型 */ public boolean search (int[] A, int target) { // write code here if(A == null || A.length <= 0) return false; int left=0,right = A.length-1; while(left<=right){ int mid = (left+right)/2; if(A[mid] == target) return true; // 如果重复跳过重复再循环一次 if (A[left] == A[mid]) { left++; continue; } if(A[left] < A[mid]){ if(A[mid]>target && A[left] <= target){ right = mid - 1; }else{ left = mid + 1; } }else{ if(A[mid] < target && A[right] >= target){ left = mid + 1; }else{ right = mid - 1; } } /*if(A[mid]>=A[right]){ if(A[mid] > target && A[right] <= target){ right = mid - 1; }else{ left = mid + 1; } }else{ if(A[mid] < target && A[left] >= target){ left = mid + 1; }else{ right = mid - 1; } }*/ } return false; } }
public boolean search(int[] A, int target) { int lo = 0; int hi = A.length - 1; while (lo <= hi) { int mid = (lo + hi) >> 1; if (target == A[mid]) return true; else if (A[lo] == A[mid] && A[mid] == A[hi]) { lo++; hi--; } else if (A[lo] <= A[mid]) { if (A[lo] <= target && target < A[mid]) hi = mid - 1; else lo = mid + 1; } else if (A[mid] <= A[hi]) { if (A[mid] < target && target <= A[hi]) lo = mid + 1; else hi = mid - 1; } } return false; }
Since we will have some duplicate elements in this problem, it is a little tricky because sometimes we cannot decide whether to go to the left side or right side. So for this condition, I have to probe both left and right side simultaneously to decide which side we need to find the number. Only in this condition, the time complexity may be O(n). The rest conditions are always O(log n).
For example:
input: 113111111111, Looking for target 3.
Is my solution correct? My code is as followed:
public class Solution { public boolean search(int[] A, int target) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int i = 0; int j = A.length - 1; while(i <= j){ int mid = (i + j) / 2; if(A[mid] == target) return true; else if(A[mid] < A[i]){ if(target > A[j]) j = mid - 1; else if(target < A[mid]) j = mid - 1; else i = mid + 1; }else if(A[mid] > A[i]){ if(target < A[mid] && target >= A[i]) j = mid - 1; else i = mid + 1; }else{ // A[mid] == A[i] if(A[mid] != A[j]) i = mid + 1; else{ boolean flag = true; for(int k = 1; mid - k >= i && mid + k <= j; k++){ if(A[mid] != A[mid - k]){ j = mid - k; flag = false; break; }else if(A[mid] != A[mid + k]){ i = mid + k; flag = false; break; } } if(flag) return false; } } } return false; } }
import java.util.*; public class Solution { public boolean search(int[] A, int target) { Arrays.sort(A); int low = 0; int high = A.length - 1; while (low <= high) { int mid = (low + high) / 2; if(A[mid] == target) return true; else if(A[mid] > target) high = mid - 1; else low = mid + 1; } return false; } }