LeetCode - #81 Search Rotation Sorted Array II


insert image description here

foreword

Our community will continue to organize Gu Yi ( Netflix growth hacker, author of "iOS interview" and ACE professional fitness coach. )'s Swift algorithm solutions into text versions for everyone to learn and read.

We have updated 80 issues of the LeetCode algorithm so far, and we will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There is not much content in each issue, and we hope that everyone can read it on the way to work and accumulate for a long time There will be a big improvement.

If you don’t accumulate steps, you can’t reach thousands of miles; if you don’t accumulate small streams, you can’t form rivers and seas. The Swift community is with you. If you have suggestions and comments, please leave a message at the end of the article, and we will try our best to meet your needs.

Difficulty Level: Moderate

1. Description

It is known that there exists an array of integers in non-descending order nums, and the values ​​in the array do not have to be different from each other.

Before being passed to the function, a rotation is performed on numssome subscript k( ) unknown in advance , so that the array becomes ( subscript counting from 0). For example, after rotation at the subscript it might become .0 <= k < nums.length[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]][0,1,2,4,4,4,5,6,6,7]5[4,5,6,6,7,0,1,2,4,4]

Given your rotated array numsand an integer target, please write a function to determine whether the given target value exists in the array. Returns if the target value exists numsin , otherwise .targettruefalse

You have to minimize the whole operation steps as much as possible.

2. Examples

Example 1

输入:nums = [2,5,6,0,0,1,2], target = 0
输出:true

Example 2

输入:nums = [2,5,6,0,0,1,2], target = 3
输出:false

Restrictions:

  • 1 <= nums.length <= 5000
  • -10^4 <= nums[i] <= 10^4
  • The title data is numsguaranteed to be rotated on a subscript unknown in advance
  • -10^4 <= target <= 10^4

Advanced:

  • This is 搜索旋转排序数组an extended topic of , in this topic numsmay contain repeated elements.
  • Will this affect the time complexity of the program? What will be the impact and why?

3. Answer

class SearchInRotatedSortedArrayII {
    
    
    func search(nums: [Int], _ target: Int) -> Bool {
    
    
        var left = 0
        var right = nums.count - 1
        var mid = 0
        
        while left <= right {
    
    
            mid = (right - left) / 2 + left
            
            if nums[mid] == target {
    
    
                return true
            }
            
            if nums[mid] > nums[left] {
    
    
                if nums[mid] > target && target >= nums[left] {
    
    
                    right = mid - 1
                } else {
    
    
                    left = mid + 1
                }
            } else if nums[mid] < nums[left]{
    
    
                if nums[mid] < target && target <= nums[right] {
    
    
                    left = mid + 1
                } else {
    
    
                    right = mid - 1
                }
            } else {
    
    
                left += 1
            }
        }
        
        return false
    }
}
  • Main idea: binary search, check if left or right is sorted, then look in the section, jump if duplicate.
  • Time complexity: O(logn)
  • Space Complexity: O(1)

The warehouse of the algorithm solution: LeetCode-Swift

Click to go to LeetCode practice

about Us

We are jointly maintained by Swift enthusiasts. We will share technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/131321886