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 8f64507

Browse files
Merge pull request #188 from gostool/leetcode0520
Leetcode0520
2 parents d9a9759 + 0ef19c8 commit 8f64507

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
func detectCapitalUse(word string) bool {
6+
wLower := strings.ToLower(word)
7+
wUpper := strings.ToUpper(word)
8+
wCaptial := strings.ToUpper(string(word[0])) + strings.ToLower(string(word[1:]))
9+
if wCaptial == word || wLower == word || wUpper == word {
10+
return true
11+
}
12+
return false
13+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question520 struct {
9+
para520
10+
ans520
11+
}
12+
13+
// para 是参数
14+
type para520 struct {
15+
word string
16+
}
17+
18+
// ans 是答案
19+
type ans520 struct {
20+
ans bool
21+
}
22+
23+
func Test_Problem520(t *testing.T) {
24+
25+
qs := []question520{
26+
27+
{
28+
para520{"USA"},
29+
ans520{true},
30+
},
31+
32+
{
33+
para520{"FlaG"},
34+
ans520{false},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 520------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans520, q.para520
42+
fmt.Printf("【input】:%v 【output】:%v\n", p, detectCapitalUse(p.word))
43+
}
44+
fmt.Printf("\n\n\n")
45+
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [520. Detect Capital](https://leetcode-cn.com/problems/detect-capital/)
2+
3+
4+
## 题目
5+
6+
We define the usage of capitals in a word to be right when one of the following cases holds:
7+
8+
All letters in this word are capitals, like "USA".
9+
10+
All letters in this word are not capitals, like "leetcode".
11+
12+
Only the first letter in this word is capital, like "Google".
13+
14+
Given a string word, return true if the usage of capitals in it is right.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: word = "USA"
20+
Output: true
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: word = "FlaG"
27+
Output: false
28+
```
29+
30+
**Constraints:**
31+
32+
- 1 <= word.length <= 100
33+
- word consists of lowercase and uppercase English letters.
34+
35+
## 题目大意
36+
37+
我们定义,在以下情况时,单词的大写用法是正确的:
38+
39+
全部字母都是大写,比如 "USA" 。
40+
单词中所有字母都不是大写,比如 "leetcode" 。
41+
如果单词不只含有一个字母,只有首字母大写,比如"Google" 。
42+
43+
给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。
44+
45+
## 解题思路
46+
47+
- 把word分别转换为全部小写wLower,全部大写wUpper,首字母大写的字符串wCaptial
48+
- 判断word是否等于wLower,wUpper,wCaptial中的一个,如果是返回true,否则返回false
49+
50+
## 代码
51+
```go
52+
package leetcode
53+
54+
import "strings"
55+
56+
func detectCapitalUse(word string) bool {
57+
wLower := strings.ToLower(word)
58+
wUpper := strings.ToUpper(word)
59+
wCaptial := strings.ToUpper(string(word[0])) + strings.ToLower(string(word[1:]))
60+
if wCaptial == word || wLower == word || wUpper == word {
61+
return true
62+
}
63+
return false
64+
}
65+
```

0 commit comments

Comments
(0)

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