diff --git a/pics/.DS_Store b/pics/.DS_Store deleted file mode 100644 index 5008ddfc..00000000 Binary files a/pics/.DS_Store and /dev/null differ diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 1c5ad2c8..9bd9ef16 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -234,8 +234,6 @@ class Solution { ``` - - Python: ```python3 class Solution: @@ -254,9 +252,6 @@ class Solution: return right + 1 ``` - -Go: - JavaScript: ```js var searchInsert = function (nums, target) { @@ -277,6 +272,42 @@ var searchInsert = function (nums, target) { }; ``` +Swift: + +```swift +// 暴力法 +func searchInsert(_ nums: [Int], _ target: Int) -> Int { + for i in 0..= target { + return i + } + } + return nums.count +} + +// 二分法 +func searchInsert(_ nums: [Int], _ target: Int) -> Int { + var left = 0 + var right = nums.count - 1 + + while left <= right { + let middle = left + ((right - left) >> 1) + + if nums[middle] > target { + right = middle - 1 + }else if nums[middle] < target { + left = middle + 1 + }else if nums[middle] == target { + return middle + } + } + + return right + 1 +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)