Leetcode 33 Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7]
might become [4,5,6,7,0,1,2]
).
You are given a target value to search. If found in the array return its index, otherwise return -1
.
You may assume no duplicate exists in the array.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1
這個題目的意思是在一個逆轉的陣列中進行二分查詢,使用的方法為虛擬節點(類似於一致性雜湊中的虛擬節點),將虛擬節點對映到真實節點即可。
1)虛擬節點法
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int low = 0 , high = n - 1;
while(low < high){//設定虛擬節點
int mid = (low + high) / 2;
if(nums[mid] > nums[high]){
low = mid + 1;
}else{
high = mid;
}
}
int rot = low;
low = 0;
high = n - 1;
while(low <= high){
int mid = (low + high) / 2;
int real = (mid + rot) % n;//虛擬節點對映到真實的節點。
if(nums[real] == target){
return real;
}else if(nums[real] < target){
low = mid + 1;
}else{
high = mid - 1;
}
}
return -1;
}
}
2)
class Solution {
public int search(int[] nums, int target) {
int l=0;
int h=nums.length-1;
while(l<=h){
int mid=(l+h)/2;
int midValue=(nums[mid]<nums[0])==(target<nums[0])?nums[mid]:target<nums[0]?Integer.MIN_VALUE:Integer.MAX_VALUE;
if(target==midValue){
return mid;
}else if(target>midValue){
l=mid+1;
}else{
h=mid-1;
}
}
return -1;
}
}
相關文章
- leetcode33_Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Search in Rotated Sorted Array II leetcode javaLeetCodeJava
- LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等LeetCodeC++
- [LeetCode] Search in Rotated Sorted Array 在旋轉有序陣列中搜尋LeetCode陣列
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 153. Find Minimum in Rotated Sorted ArrayLeetCode
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋轉有序矩陣中搜尋矩陣
- Leetcode Convert Sorted Array to Binary Search TreeLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- [LeetCode] 702. Search in a Sorted Array of Unknown SizeLeetCode
- Convert Sorted Array to Binary Search Tree leetcode javaLeetCodeJava
- [LeetCode] Find Minimum in Rotated Sorted Array (包含遞增和遞減旋轉)LeetCode
- Leetcode Merge Sorted ArrayLeetCode
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode-Merge Sorted ArrayLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 88. Merge Sorted ArrayLeetCode
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Convert Sorted Array to BSTLeetCode
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- LeetCode解題報告 108. Convert Sorted Array to Binary Search Tree [medium]LeetCode
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- 【LeetCode】Convert Sorted List to Binary Search TreeLeetCode
- Leetcode Convert Sorted List to Binary Search TreeLeetCode
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST