We define the usage of capitals in a word to be right when one of the following cases holds:
All letters in this word are capitals, like “USA”.
All letters in this word are not capitals, like “leetcode”.
Only the first letter in this word is capital, like “Google”.
Given a string word, return true if the usage of capitals in it is right.
-
全部字母都是大写,比如 “USA” 。
-
单词中所有字母都不是大写,比如 “leetcode” 。
-
如果单词不只含有一个字母,只有首字母大写, 比如 “Google” 。
示例
示例 1:
输入:word = "USA"
输出:true
示例 2:
输入:word = "FlaG"
输出:false
解题
class Solution {
public:
bool detectCapitalUse(string word) {
int upCt=0;
for(int i=0;i<word.length();i++) {
if(isupper(word[i])) {
if(upCt<i) {
return false;
}
upCt++;
}
}
return upCt==word.length() || upCt<=1;
}
};
本篇文章来源于微信公众号:程序IT圈
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/19301.html