Swift 64ms O(n)


  • 0
    C
    class Solution {
        func searchInsert(nums: [Int], _ target: Int) -> Int {
            var index = 0
            while( index < nums.count ) {
                if target > nums[index] {
                    index++
                } else if target < nums[index] {
                    return index
                } else {
                    return index
                }
            }
            return index
        }
    }

Log in to reply
 

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