Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b3fb37

Browse files
committed
commit solution 34
1 parent 431e025 commit 4b3fb37

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

‎index-tags.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [15](https://leetcode-cn.com/problems/3sum) | [三数之和](/solution/1-99/0015.3sum/) | `数组`,`双指针` | <font color=blue>中等</font> ||
1212
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1313
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
14+
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> ||
1415
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
1516
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
1617
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
@@ -74,6 +75,7 @@
7475
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7576
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7677
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> ||
78+
| [299](https://leetcode-cn.com/problems/bulls-and-cows) | [猜数字游戏](/solution/200-299/0299.bulls-and-cows/) | `哈希表` | <font color=green>简单</font> ||
7779

7880
#### ****
7981

@@ -117,6 +119,7 @@
117119

118120
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
119121
| --- | --- | --- | --- | --- |
122+
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> ||
120123
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
121124
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
122125
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||

‎index-type.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> ||
9696
| [292](https://leetcode-cn.com/problems/nim-game) | [nim 游戏](/solution/200-299/0292.nim-game/) | `脑筋急转弯`,`极小化极大` | <font color=green>简单</font> ||
9797
| [293](https://leetcode-cn.com/problems/flip-game) | [翻转游戏](/solution/200-299/0293.flip-game/) | `字符串` | <font color=green>简单</font> |
98-
| [299](https://leetcode-cn.com/problems/bulls-and-cows) | [猜数字游戏](/solution/200-299/0299.bulls-and-cows/) | `哈希表` | <font color=green>简单</font> |
98+
| [299](https://leetcode-cn.com/problems/bulls-and-cows) | [猜数字游戏](/solution/200-299/0299.bulls-and-cows/) | `哈希表` | <font color=green>简单</font> ||
9999
| [303](https://leetcode-cn.com/problems/range-sum-query-immutable) | [区域和检索 - 数组不可变](/solution/300-399/0303.range-sum-query---immutable/) | `动态规划` | <font color=green>简单</font> |
100100
| [326](https://leetcode-cn.com/problems/power-of-three) | [3的幂](/solution/300-399/0326.power-of-three/) | `数学` | <font color=green>简单</font> |
101101
| [339](https://leetcode-cn.com/problems/nested-list-weight-sum) | [嵌套列表权重和](/solution/300-399/0339.nested-list-weight-sum/) | `深度优先搜索` | <font color=green>简单</font> |
@@ -422,7 +422,7 @@
422422
| [29](https://leetcode-cn.com/problems/divide-two-integers) | [两数相除](/solution/1-99/0029.divide-two-integers/) | `数学`,`二分查找` | <font color=blue>中等</font> |
423423
| [31](https://leetcode-cn.com/problems/next-permutation) | [下一个排列](/solution/1-99/0031.next-permutation/) | `数组` | <font color=blue>中等</font> |
424424
| [33](https://leetcode-cn.com/problems/search-in-rotated-sorted-array) | [搜索旋转排序数组](/solution/1-99/0033.search-in-rotated-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> |
425-
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> |
425+
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> ||
426426
| [36](https://leetcode-cn.com/problems/valid-sudoku) | [有效的数独](/solution/1-99/0036.valid-sudoku/) | `哈希表` | <font color=blue>中等</font> ||
427427
| [39](https://leetcode-cn.com/problems/combination-sum) | [组合总和](/solution/1-99/0039.combination-sum/) | `数组`,`回溯算法` | <font color=blue>中等</font> |
428428
| [40](https://leetcode-cn.com/problems/combination-sum-ii) | [组合总和 ii](/solution/1-99/0040.combination-sum-ii/) | `数组`,`回溯算法` | <font color=blue>中等</font> |

‎solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [34. 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
66

77
<p>你的算法时间复杂度必须是&nbsp;<em>O</em>(log <em>n</em>) 级别。</p>
@@ -25,12 +25,30 @@
2525

2626
### 具体解法
2727

28-
<!-- tabs:start -->
29-
3028
#### **Golang**
3129
```go
32-
30+
func searchRange(nums []int, target int) []int {
31+
left, right := 0, len(nums)-1
32+
for left <= right {
33+
mid := left + (right-left)/2
34+
if nums[mid] < target {
35+
left = mid + 1
36+
} else if nums[mid] > target {
37+
right = mid - 1
38+
} else {
39+
res := []int{mid, mid}
40+
for res[0]-1 >= 0 && nums[res[0]-1] == target {
41+
res[0]--
42+
}
43+
for res[1]+1 < len(nums) && nums[res[1]+1] == target {
44+
res[1]++
45+
}
46+
return res
47+
}
48+
}
49+
50+
return []int{-1, -1}
51+
}
3352
```
3453

35-
<!-- tabs:end -->
3654

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=34 lang=golang
5+
*
6+
* [34] 在排序数组中查找元素的第一个和最后一个位置
7+
*/
8+
9+
// @lc code=start
10+
func searchRange(nums []int, target int) []int {
11+
left, right := 0, len(nums)-1
12+
for left <= right {
13+
mid := left + (right-left)/2
14+
if nums[mid] < target {
15+
left = mid + 1
16+
} else if nums[mid] > target {
17+
right = mid - 1
18+
} else {
19+
res := []int{mid, mid}
20+
for res[0]-1 >= 0 && nums[res[0]-1] == target {
21+
res[0]--
22+
}
23+
for res[1]+1 < len(nums) && nums[res[1]+1] == target {
24+
res[1]++
25+
}
26+
return res
27+
}
28+
}
29+
30+
return []int{-1, -1}
31+
}
32+
33+
// @lc code=end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSearchRange(t *testing.T) {
8+
var nums []int
9+
var target int
10+
var ret []int
11+
12+
target = 6
13+
nums = []int{5, 7, 7, 8, 8, 10}
14+
ret = []int{-1, -1}
15+
for i, v := range searchRange(nums, target) {
16+
if ret[i] != v {
17+
t.Fatalf("case fails %v\n", ret)
18+
}
19+
}
20+
21+
target = 8
22+
nums = []int{5, 7, 7, 8, 8, 10}
23+
ret = []int{3, 4}
24+
for i, v := range searchRange(nums, target) {
25+
if ret[i] != v {
26+
t.Fatalf("case fails %v\n", ret)
27+
}
28+
}
29+
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /