Swift solution - Binary Search


  • 0
    class Solution {
        func findMin(_ nums: [Int]) -> Int {
            if nums.count == 0 {
                return 0
            }
            
            var left = 0
            var right = nums.count - 1
            var middle = 0
            
            while left < right {
                middle = (left + right) / 2
                if nums[middle] > nums[right] {
                    left = middle + 1
                } else {
                    right = middle
                }
            }
            
            return nums[left]
        }
    }
    

Log in to reply
 

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