Skip to content

Commit fb6f2fe

Browse files
committed
-
1 parent 52bb980 commit fb6f2fe

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

C++/520. Detect Capital.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

0 commit comments

Comments
 (0)