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;
}
}
相關文章
- # Search in Rotated Sorted Array
- LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等LeetCodeC++
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- [LeetCode] 702. Search in a Sorted Array of Unknown SizeLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- Find Minimum in Rotated Sorted Array I & II
- 【刷題】Search in a Big Sorted Array
- 108-Convert Sorted Array to Binary Search Tree
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- Leetcode 88. Merge Sorted ArrayLeetCode
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- 88. Merge Sorted Array
- 977. Squares of a Sorted Array
- Remove-duplicates-from-sorted-arrayREM
- Leetcode Word SearchLeetCode
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- Leetcode Sort ArrayLeetCode
- PHP array_search 和 in_array 函式效率問題PHP函式
- Leetcode 23 Merge k Sorted ListsLeetCode
- Leetcode 21 Merge Two Sorted ListsLeetCode
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- Leetcode 35 Search Insert PositionLeetCode
- Rotate Array@LeetCodeLeetCode
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST
- LeetCode 21. Merge Two Sorted ListsLeetCode
- LeetCode 4. Median of Two Sorted ArraysLeetCode
- 【Leetcode】23. Merge k Sorted ListsLeetCode
- [LeetCode] 212. Word Search IILeetCode
- LeetCode 35. Search Insert PositionLeetCode
- LeetCode Kth Largest Element in an ArrayLeetCode
- LeetCode Patching Array All In OneLeetCode