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 6e2379f

Browse files
author
Shuo
authored
Merge pull request #658 from openset/develop
Add: Find Smallest Letter Greater Than Target
2 parents ce67878 + ad37bc3 commit 6e2379f

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
package find_smallest_letter_greater_than_target
1+
package problem_744
2+
3+
func nextGreatestLetter(letters []byte, target byte) byte {
4+
for _, v := range letters {
5+
if v > target {
6+
return v
7+
}
8+
}
9+
return letters[0]
10+
}
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
package find_smallest_letter_greater_than_target
1+
package problem_744
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []byte
7+
target byte
8+
expected byte
9+
}
10+
11+
func TestNextGreatestLetter(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
input: []byte{'c', 'f', 'j'},
15+
target: 'a',
16+
expected: 'c',
17+
},
18+
{
19+
input: []byte{'c', 'f', 'j'},
20+
target: 'c',
21+
expected: 'f',
22+
},
23+
{
24+
input: []byte{'c', 'f', 'j'},
25+
target: 'd',
26+
expected: 'f',
27+
},
28+
{
29+
input: []byte{'c', 'f', 'j'},
30+
target: 'g',
31+
expected: 'j',
32+
},
33+
{
34+
input: []byte{'c', 'f', 'j'},
35+
target: 'j',
36+
expected: 'c',
37+
},
38+
{
39+
input: []byte{'c', 'f', 'j'},
40+
target: 'k',
41+
expected: 'c',
42+
},
43+
}
44+
for _, tc := range tests {
45+
output := nextGreatestLetter(tc.input, tc.target)
46+
if output != tc.expected {
47+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
48+
}
49+
}
50+
}

0 commit comments

Comments
(0)

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