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] =?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