File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ 520 . Detect Capital
2
+ Given a word, you need to judge whether the usage of capitals in it is right or not .
3
+
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 if it has more than one letter, like " Google" .
9
+ Otherwise, we define that this word doesn' t use capitals in a right way.
10
+ Example 1:
11
+ Input: "USA"
12
+ Output: True
13
+ Example 2:
14
+ Input: "FlaG"
15
+ Output: False
16
+
17
+ 题目大意:判断一个字母是否大小写正确:要么全是大写,要么全是小写,或者首字母大写其他小写,否则不满足题意~
18
+ 分析:判断word[0]和word[1]的大小写,如果word[0]是小写,那后面必须是小写,如果word[0]是大写word[1]是小写,那后面也必须是小写,如果word[0]是大写word[1]也是大写那么后面必须都是大写~
19
+
20
+ class Solution {
21
+ public:
22
+ bool detectCapitalUse(string word) {
23
+ if (word.length() <= 1) return true;
24
+ if (islower(word[0]) || (isupper(word[0]) && islower(word[1]))) {
25
+ for (int i = 1; i < word.length(); i++)
26
+ if (isupper(word[i])) return false;
27
+ } else {
28
+ for (int i = 1; i < word.length(); i++)
29
+ if (islower(word[i])) return false;
30
+ }
31
+ return true;
32
+ }
33
+ };
You can’t perform that action at this time.
0 commit comments