java 33.在Rotated Sorted Array.java中搜索

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 33.在Rotated Sorted Array.java中搜索相关的知识,希望对你有一定的参考价值。

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:
            return -1;
        
        start = 0;
        virtualEnd = len(nums) - 1;
        for i in range(len(nums)):
            if i > 0 and nums[i] < nums[i - 1]:
                start = i;
                break;
                
        
        virtualEnd = start + len(nums) - 1; 
        
        while(start <= virtualEnd):
            
            midpoint = (start + virtualEnd) / 2 ; 
            if target == nums[midpoint % len(nums)]:
                return midpoint % len(nums);
            elif target < nums[midpoint % len(nums)]:
                virtualEnd = midpoint - 1 ;
            else:
                start = midpoint + 1;
                
        return -1;

##############################################################
#Using once binary search

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:
            return -1;
        
        start = 0;
        end = len(nums) - 1;
        
        while(start <= end):
            
            midpoint = (start + end) / 2 ; 
            if target == nums[midpoint]:
                return midpoint;
                
            if nums[start] <= nums[midpoint]:
                if target < nums[midpoint] and target >= nums[start]:
                    end = midpoint - 1;
                else:
                    start = midpoint + 1;
                    
            if nums[midpoint] <= nums[end]:
                if target > nums[midpoint] and target <= nums[end]:
                    start = midpoint + 1;
                else:
                    end = midpoint - 1;
        
        return -1;
        
            
"""
TESTCASES:
Input:
[]
5
[1]
1
[1]
0
[1,3]
0
[4,5,6,7,0,1,2]
3
[4,5,6,7,0,1,2]
6
[0,1,2,3,4,5,6]
5
[0,1,2,3,4,5,6]
3
[0,1,2,3,4,5,6]
0
[0,1,2,3,4,5,6]
7

Output:
-1
0
-1
-1
-1
2
5
3
0
-1
"""
            
            
public class Solution {
    public int search(int[] nums, int target) {
        if(nums == null || nums.length == 0) return -1;
        
        int start = 0;
        int end = nums.length - 1;
        
        while(start <= end){
            int mid = (start + end) / 2;
            
            if(nums[mid] == target) {
                return mid;
            } 
            
            if (nums[start] <= nums[mid]){
                if (target < nums[mid] && target >= nums[start]){
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
            
            if(nums[mid] <= nums[end]){
                if (target > nums[mid] && target <= nums[end]){
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
        }
        return -1;
    }
}

以上是关于java 33.在Rotated Sorted Array.java中搜索的主要内容,如果未能解决你的问题,请参考以下文章

java 33.在Rotated Sorted Array.java中搜索

java 33.在Rotated Sorted Array.java中搜索

java 33.在Rotated Sorted Array.java中搜索

java 33.在Rotated Sorted Array.java中搜索

33. Search in Rotated Sorted Array *HARD*

LeetCode第[33]题(Java):Search in Rotated Sorted Array