Find Minimum in Rotated Sorted Array II.swift


  • 0
    R

    class Solution {
    func findMin(nums:[Int]) -> Int {
    var left = 0
    var right = nums.count - 1
    var mid = 0
    var minVal = Int.max
    if nums.count == 0 {
    return 0
    }
    while left + 1 < right {
    mid = (right - left) / 2 + left
    if nums[mid] > nums[left] {
    minVal = min(nums[left], minVal)
    left = mid + 1
    } else if nums[mid] < nums[left] {
    minVal = min(nums[mid], minVal)
    right = mid - 1
    } else {
    left += 1
    }
    }

        return min(minVal, nums[left], nums[right])
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.