Skip to content

Commit 8349570

Browse files
committed
添加 problem 744
1 parent 56c53c6 commit 8349570

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func nextGreatestLetter(letters []byte, target byte) byte {
4+
low, high := 0, len(letters)-1
5+
for low <= high {
6+
mid := low + (high-low)>>1
7+
if letters[mid] > target {
8+
high = mid - 1
9+
} else {
10+
low = mid + 1
11+
}
12+
}
13+
find := letters[low%len(letters)]
14+
if find <= target {
15+
return letters[0]
16+
}
17+
return find
18+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question744 struct {
9+
para744
10+
ans744
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para744 struct {
16+
letters []byte
17+
target byte
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans744 struct {
23+
one byte
24+
}
25+
26+
func Test_Problem744(t *testing.T) {
27+
28+
qs := []question744{
29+
30+
question744{
31+
para744{[]byte{'c', 'f', 'j'}, 'a'},
32+
ans744{'c'},
33+
},
34+
35+
question744{
36+
para744{[]byte{'c', 'f', 'j'}, 'c'},
37+
ans744{'f'},
38+
},
39+
40+
question744{
41+
para744{[]byte{'c', 'f', 'j'}, 'd'},
42+
ans744{'f'},
43+
},
44+
45+
question744{
46+
para744{[]byte{'c', 'f', 'j'}, 'g'},
47+
ans744{'j'},
48+
},
49+
50+
question744{
51+
para744{[]byte{'c', 'f', 'j'}, 'j'},
52+
ans744{'c'},
53+
},
54+
55+
question744{
56+
para744{[]byte{'c', 'f', 'j'}, 'k'},
57+
ans744{'c'},
58+
},
59+
}
60+
61+
fmt.Printf("------------------------Leetcode Problem 744------------------------\n")
62+
63+
for _, q := range qs {
64+
_, p := q.ans744, q.para744
65+
fmt.Printf("【input】:%v 【output】:%v\n", p, nextGreatestLetter(p.letters, p.target))
66+
}
67+
fmt.Printf("\n\n\n")
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [744. Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)
2+
3+
4+
## 题目:
5+
6+
Given a list of sorted characters `letters` containing only lowercase letters, and given a target letter `target`, find the smallest element in the list that is larger than the given target.
7+
8+
Letters also wrap around. For example, if the target is `target = 'z'` and `letters = ['a', 'b']`, the answer is `'a'`.
9+
10+
**Examples:**
11+
12+
Input:
13+
letters = ["c", "f", "j"]
14+
target = "a"
15+
Output: "c"
16+
17+
Input:
18+
letters = ["c", "f", "j"]
19+
target = "c"
20+
Output: "f"
21+
22+
Input:
23+
letters = ["c", "f", "j"]
24+
target = "d"
25+
Output: "f"
26+
27+
Input:
28+
letters = ["c", "f", "j"]
29+
target = "g"
30+
Output: "j"
31+
32+
Input:
33+
letters = ["c", "f", "j"]
34+
target = "j"
35+
Output: "c"
36+
37+
Input:
38+
letters = ["c", "f", "j"]
39+
target = "k"
40+
Output: "c"
41+
42+
**Note:**
43+
44+
1. `letters` has a length in range `[2, 10000]`.
45+
2. `letters` consists of lowercase letters, and contains at least 2 unique letters.
46+
3. `target` is a lowercase letter.
47+
48+
49+
## 题目大意
50+
51+
给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母。
52+
53+
数组里字母的顺序是循环的。举个例子,如果目标字母target = 'z' 并且有序数组为 letters = ['a', 'b'],则答案返回 'a'。
54+
55+
注:
56+
57+
1. letters长度范围在[2, 10000]区间内。
58+
2. letters 仅由小写字母组成,最少包含两个不同的字母。
59+
3. 目标字母target 是一个小写字母。
60+
61+
62+
63+
## 解题思路
64+
65+
- 给出一个字节数组,在这个字节数组中查找在 target 后面的第一个字母。数组是环形的。
66+
- 这一题也是二分搜索的题目,先在数组里面查找 target,如果找到了,取这个字母的后一个字母。如果没有找到,就取 low 下标的那个字母。注意数组是环形的,所以最后结果需要对下标取余。

0 commit comments

Comments
 (0)