[go: nahoru, domu]

Skip to content

Commit

Permalink
Submit 35_SearchInsertPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 17, 2020
1 parent e390462 commit a2cb763
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//: [Previous](@previous)

import Foundation

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

/// Tests
let s = Solution()
s.searchInsert([1,3,5,6], 5) == 2
s.searchInsert([1,3,5,6], 2) == 1
s.searchInsert([1,3,5,6], 7) == 4
s.searchInsert([1,3,5,6], 0) == 0

//: [Next](@next)

0 comments on commit a2cb763

Please sign in to comment.