Skip to content

Commit 0958b19

Browse files
committed
✨520.检测大写字母
1 parent 1b61253 commit 0958b19

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [350.两个数组的交集 II](leetcode/easy/350.go)
6060
- [387.字符串中的第一个唯一字符](leetcode/easy/387.go)
6161
- [415.字符串相加](leetcode/easy/t415/solution.go)
62+
- [520.检测大写字母](leetcode/easy/t520/solution.go)
6263
- [521.最长特殊序列 Ⅰ](leetcode/easy/521.go)
6364
- [543.二叉树的直径](leetcode/easy/t543/solution.go)
6465
- [566.重塑矩阵](leetcode/easy/556.go)

leetcode/easy/t520/solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package t520
2+
3+
// 520. 检测大写字母
4+
// https://leetcode.cn/problems/detect-capital
5+
func detectCapitalUse(word string) bool {
6+
count := 0
7+
for _, c := range word {
8+
if c >= 'A' && c <= 'Z' {
9+
count++
10+
}
11+
}
12+
if count == 0 || count == len(word) || (count == 1 && word[0] >= 'A' && word[0] <= 'Z') {
13+
return true
14+
}
15+
return false
16+
}

leetcode/easy/t520/solution_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package t520
2+
3+
import "testing"
4+
5+
func TestDetectCapitalUse(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
input string
9+
want bool
10+
}{
11+
{
12+
name: "Case 1",
13+
input: "USA",
14+
want: true,
15+
},
16+
{
17+
name: "Case 2",
18+
input: "FlaG",
19+
want: false,
20+
},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
if got := detectCapitalUse(tt.input); got != tt.want {
26+
t.Errorf("detectCapitalUse() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)