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 99ec36a

Browse files
committed
Merge pull request #392 from 0xff-dev/520
Add solution and test-cases for problem 520
2 parents 26cb400 + 47aa192 commit 99ec36a

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

‎leetcode/501-600/0520.Detect-Capital/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [520.Detect Capital][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
We define the usage of capitals in a word to be right when one of the following cases holds:
5+
6+
- All letters in this word are capitals, like `"USA"`.
7+
- All letters in this word are not capitals, like `"leetcode"`.
8+
- Only the first letter in this word is capital, like `"Google"`.
9+
10+
Given a string `word`, return `true` if the usage of capitals in it is right.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: word = "USA"
16+
Output: true
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Detect Capital
23-
```go
2421
```
25-
22+
Input: word = "FlaG"
23+
Output: false
24+
```
2625

2726
## 结语
2827

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(word string) bool {
4+
length := len(word)
5+
if length <= 1 {
6+
return true
7+
}
8+
cap := false
9+
if word[0] >= 65 && word[0] <= 90 {
10+
cap = true
11+
}
12+
13+
haveCap, haveLow := false, false
14+
for idx := 1; idx < length; idx++ {
15+
if word[idx] >= 65 && word[idx] <= 90 {
16+
haveCap = true
17+
} else {
18+
haveLow = true
19+
}
20+
if haveCap && haveLow {
21+
break
22+
}
23+
}
24+
if cap && !(haveCap && haveLow) {
25+
return true
26+
}
27+
return haveLow && !haveCap
528
}

‎leetcode/501-600/0520.Detect-Capital/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "leetcode", true},
17+
{"TestCase2", "USA", true},
18+
{"TestCase3", "FlaG", false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
(0)

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