Skip to content

Commit ff67f50

Browse files
committed
feat: commit solution 35
1 parent 7fffbe0 commit ff67f50

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

_sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
1818
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
1919
- [28.实现 strStr() ✅](solution/1-99/0028.implement-strstr/)
20+
- [35.搜索插入位置 ✅](solution/1-99/0035.search-insert-position/)
2021
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
2122
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
2223

index-tags.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
2727
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
2828
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
29+
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
2930
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3031

3132
#### **哈希表**
@@ -58,6 +59,12 @@
5859
| --- | --- | --- | --- | --- |
5960
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> ||
6061

62+
#### **二分查找**
63+
64+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
65+
| --- | --- | --- | --- | --- |
66+
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
67+
6168
#### **排序**
6269

6370
#### **队列**
@@ -66,8 +73,6 @@
6673

6774
#### ****
6875

69-
#### **二分查找**
70-
7176
#### **递归**
7277

7378
#### **深度优先搜素**

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1818
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
1919
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
20-
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> |
20+
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |
2222
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> |
2323
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [35.搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p>
7+
8+
<p>你可以假设数组中无重复元素。</p>
9+
10+
<p><strong>示例 1:</strong></p>
11+
12+
<pre><strong>输入:</strong> [1,3,5,6], 5
13+
<strong>输出:</strong> 2
14+
</pre>
15+
16+
<p><strong>示例&nbsp;2:</strong></p>
17+
18+
<pre><strong>输入:</strong> [1,3,5,6], 2
19+
<strong>输出:</strong> 1
20+
</pre>
21+
22+
<p><strong>示例 3:</strong></p>
23+
24+
<pre><strong>输入:</strong> [1,3,5,6], 7
25+
<strong>输出:</strong> 4
26+
</pre>
27+
28+
<p><strong>示例 4:</strong></p>
29+
30+
<pre><strong>输入:</strong> [1,3,5,6], 0
31+
<strong>输出:</strong> 0
32+
</pre>
33+
</div>
34+
35+
36+
### 解题思路
37+
38+
![](http://lc-photo.xwlin.com/35.png)
39+
40+
### 代码实现
41+
42+
<!-- tabs:start -->
43+
44+
#### **Golang**
45+
```go
46+
func searchInsert(nums []int, target int) int {
47+
left := 0
48+
right := len(nums) - 1
49+
for left <= right {
50+
mid := (left + right) / 2
51+
if nums[mid] == target {
52+
return mid
53+
} else if nums[mid] > target {
54+
right = mid - 1
55+
} else {
56+
left = mid + 1
57+
}
58+
}
59+
return left
60+
}
61+
```
62+
63+
64+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=35 lang=golang
5+
*
6+
* [35] 搜索插入位置
7+
*/
8+
9+
// @lc code=start
10+
11+
func searchInsert(nums []int, target int) int {
12+
left := 0
13+
right := len(nums) - 1
14+
for left <= right {
15+
mid := (left + right) / 2
16+
if nums[mid] == target {
17+
return mid
18+
} else if nums[mid] > target {
19+
right = mid - 1
20+
} else {
21+
left = mid + 1
22+
}
23+
}
24+
return left
25+
}
26+
27+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSearchInsert(t *testing.T) {
8+
var nums []int
9+
var target int
10+
var ret int
11+
12+
nums = []int{1, 3}
13+
target = 2
14+
ret = 1
15+
if ret != searchInsert(nums, target) {
16+
t.Fatalf("case fails: %v\n", ret)
17+
}
18+
19+
nums = []int{1, 3, 5, 6}
20+
target = 5
21+
ret = 2
22+
if ret != searchInsert(nums, target) {
23+
t.Fatalf("case fails: %v\n", ret)
24+
}
25+
26+
nums = []int{1, 3, 5, 6}
27+
target = 2
28+
ret = 1
29+
if ret != searchInsert(nums, target) {
30+
t.Fatalf("case fails: %v\n", ret)
31+
}
32+
33+
nums = []int{1, 3, 5, 6}
34+
target = 7
35+
ret = 4
36+
if ret != searchInsert(nums, target) {
37+
t.Fatalf("case fails: %v\n", ret)
38+
}
39+
40+
nums = []int{1, 3, 5, 6}
41+
target = 0
42+
ret = 0
43+
if ret != searchInsert(nums, target) {
44+
t.Fatalf("case fails: %v\n", ret)
45+
}
46+
}

0 commit comments

Comments
 (0)