File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
leetcode/0520.Detect-Capital Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments