Solution in Swift


  • 0
    R
    class Solution {
        func searchInsert(_ nums: [Int], _ target: Int) -> Int {
            var answer = 0
            for i in 0 ..< nums.count{
                if target <= nums[0]{
                    answer = 0
                }else if target <= nums[i]   && target > nums[i-1]{
                    
                    answer = i
                }else if target > nums[i]{
                    answer = i + 1
                }
                
            }
            
            print(answer)
            return answer
            
        }
    }
    

Log in to reply
 

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