diff --git a/Search/SearchInsertPosition.swift b/Search/SearchInsertPosition.swift index fa454352..6d0f4f81 100644 --- a/Search/SearchInsertPosition.swift +++ b/Search/SearchInsertPosition.swift @@ -35,4 +35,32 @@ class SearchInsertPosition { return right } -} \ No newline at end of file + + func searchInsertI(nums: [Int], _ target: Int) -> Int { + guard nums.count > 0 else { + return 0 + } + + var left = 0 + var right = nums.count - 1 + + var res = 0 + while left <= right { + let mid = left + (right - left) / 2 + if nums[mid] == target { + + return mid + } else if nums[mid] < target { + + res = mid + 1 + left = mid + 1 + } else { + + res = mid + right = mid - 1 + } + } + + return res + } +}