算法

旋转数组

2020-11-08  本文已影响0人  小鱼嘻嘻

问题1

旋转数组最小数

原理

代码

class Solution {
    public int findMin(int[] nums) {
        int low = 0;
        int high = nums.length-1;
        while (low<high){
            int mid = low+(high-low)/2;
            if(nums[mid]>nums[high]){
                low = mid+1;
            }else{
                high = mid;
            }
        }
        return nums[low];
    }
}

注意事项

问题2

旋转数组最小数,包含重复数

原理

代码

class Solution {
    public int findMin(int[] nums) {
        int low = 0;
        int high = nums.length-1;
        while (low<high){
            int mid = low+(high-low)/2;
            if(nums[mid]>nums[high]){
                low = mid+1;
            }else if(nums[mid]>nums[high]){
                high = mid;
            }else{
                high--;
            }
        }
        return nums[low];
    }
}

注意事项

问题3

旋转数组寻找目标数

原理

代码

class Solution {
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;

        while (low<=high){
            int mid = low+(high-low)/2;
            if (nums[mid]==target){
                return  mid;
            }
            // 右半边有序
            if (nums[mid]<nums[high]){
                if(nums[mid]<target&&target<=nums[high]){
                    low = mid+1;
                }else{
                    high = mid-1;
                }
            }else{
                if (nums[low]<=target&&target<nums[mid]){
                    high = mid-1;
                }else{
                    low = mid+1;
                }
            }
        }
        return -1;
    }
}

注意事项

问题4

旋转数组寻找目标数,包含重复数

原理

代码

class Solution {
    public boolean search(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;
        while (low<=high){
            int mid = low +(high-low)/2;
            if(nums[mid]==target){
                return  true;
            }
            if (nums[mid]<nums[high]){
                if (nums[mid]<target&&target<=nums[high]){
                    low = mid+1;
                }else{
                    high = mid-1;
                }
            }else if(nums[mid]>nums[high]){
                if (nums[low]<=target&&target<nums[mid]){
                    high = mid-1;
                }else{
                    low = mid+1;
                }
            }else {
               high--;
            }
        }
        return false;
    }
}

注意事项

问题5

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置

原理

代码

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int left = binaryLeft(nums,target);
        if (left==-1){
            return new int[]{-1,-1};
        }
        int right = binaryRight(nums,target);
        return  new int[]{left,right};
    }

    private int  binaryLeft(int[] nums,int target){
        int low = 0;
        int high = nums.length-1;
        while (low<=high){
            int mid = low +(high-low)/2;

            if (nums[mid]<target){
                low = mid+1;
            }else if(nums[mid]>target){
                high = mid-1;
            }else if(nums[mid]==target){
                high = mid-1;
            }
        }
        if (low != nums.length && nums[low] == target) {
            return low;
        }
        return -1;
    }
    private int  binaryRight(int[] nums,int target){
        int low = 0;
        int high = nums.length-1;
        while (low<=high){
            int mid = low +(high-low)/2;

            if (nums[mid]<target){
                low = mid+1;
            }else if(nums[mid]>target){
                high = mid-1;
            }else if(nums[mid]==target){
                low = mid+1;
            }
        }
        return high;
    }
}

注意事项

问题6

两个有序数组找中位数

原理

代码


注意事项

上一篇下一篇

猜你喜欢

热点阅读