From 92ba20009d5482ecda032ecad87f81d7fa35dc09 Mon Sep 17 00:00:00 2001 From: perfumescent <31856209+perfumescent@users.noreply.github.com> Date: Sun, 10 Oct 2021 17:55:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00035=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=20golang=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 593e3fe5..914c2679 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -232,7 +232,24 @@ class Solution { } } ``` - +Golang: +```golang +// 第一种二分法 +func searchInsert(nums []int, target int) int { + l, r := 0, len(nums) - 1 + for l <= r{ + m := l + (r - l)/2 + if nums[m] == target{ + return m + }else if nums[m] > target{ + r = m - 1 + }else{ + l = m + 1 + } + } + return r + 1 +} +``` Python: ```python3 From ffb43ada5128eeabcf53c8dcfe802513fe3d9624 Mon Sep 17 00:00:00 2001 From: perfumescent <31856209+perfumescent@users.noreply.github.com> Date: Sun, 10 Oct 2021 18:32:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=860034=E7=9A=84=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E6=B7=BB=E5=8A=A0=E8=87=B30704=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=A2=98=E7=9B=AE=E6=8E=A8=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0704.二分查找.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index e1900276..1cdc5896 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -140,7 +140,7 @@ public: ## 相关题目推荐 * [35.搜索插入位置](https://programmercarl.com/0035.搜索插入位置.html) -* 34.在排序数组中查找元素的第一个和最后一个位置 +* [34.在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html) * 69.x 的平方根 * 367.有效的完全平方数