leetcode 编程题 系列 (二分查找)旋转数组的查找、重复数字 33 153

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.

自己的代码有错。先找到最小的位置在哪,然后根据target的位置进行遍历

class Solution {
public:
    int search(vector& nums, int target) {
        int first=0,last=nums.size()-1;
        if(nums.size()==0) return -1;
        int realfirst=0,reallast=last;
        if(nums[first]>nums[last])
        {
            int maxposition=searchmin(nums)-1;
            if(nums[last]>=target)
            {
                realfirst=maxposition+1;
            }
            else 
            {
                reallast=maxposition;
            }
        }
        while(realfirstnums[middle]) realfirst=middle+1;
            else if(target&nums){
       int first=0,last=nums.size()-1;
       int middle=(first+last)/2;
        while(firstnums[last])
        {
            if(first+1==last) return last; 
            if(nums[middle]>nums[last]) first=middle;
            else if(nums[middle]<=nums[last]) last=middle;
        }
        else
           return first;
        return first;
    }
};
别人的方法:

class Solution {
public:
    int search(vector& nums, int target) {
        int lo=0;
        int hi=nums.size()-1;
        if(nums.size()==0) return -1;
        while(lo=nums[lo]&&targetnums[mid]&&target<=nums[hi])//在这个递增区间内
                {
                    lo=mid+1;
                }
                else{
                    hi=mid-1;
                }
            }
        }
        return nums[lo] == target ? lo : -1;
        
    }
};

153. Find Minimum in Rotated Sorted Array 旋转数组的最小数字 无重复 剑 11

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).

Find the minimum element.

You may assume no duplicate exists in the array.

在一个原来排好序的数组中找

class Solution {
public:
    int findMin(vector& nums) {
        int n=nums.size();
        if(n==1) return nums[0];
        int left=0;
        int right=n-1;
        int mid=0;
        if(nums[0]nums[left])
                left=mid;
            else if(nums[mid]


154. Find Minimum in Rotated Sorted Array II  剑 11

旋转数组的最小数字 有重复

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).

Find the minimum element.

The array may contain duplicates.

如果有重复的数,该怎么办?

class Solution {
public:
    int findMin(vector& nums) {
        int n=nums.size();
        if(n==1) return nums[0];
        int left=0;
        int right=n-1;
        int mid=0;
        if(nums[0]=nums[left])
                left=mid;
            else if(nums[mid]<=nums[right])
                right=mid;
        }
        return nums[mid];
    }
    int doInOrder(vector& nums,int left,int right){
        int res=nums[left];
        for(int i=left+1;i<=right;i++)
        {
            if(res>nums[i])
                res=nums[i];
        }
        return res;
    }
};

另外一个解答,思路类似:

class Solution {
public:
    int findMin(vector &num) {
        int lo = 0;
        int hi = num.size() - 1;
        int mid = 0;
        
        while(lo < hi) {
            mid = lo + (hi - lo) / 2;
            
            if (num[mid] > num[hi]) {
                lo = mid + 1;
            }
            else if (num[mid] < num[hi]) {
                hi = mid;
            }
            else { // when num[mid] and num[hi] are same
                hi--;
            }
        }
        return num[lo];
    }
};


 

你可能感兴趣的:(leetcode,leetcode/系列题目集锦)