方法一:
判断否的情况:
class Solution {
public:
bool detectCapitalUse(string word) {
bool tagfirst=isupper(word[0]);
bool tagsecond=isupper(word[1]);
if(!tagfirst&&tagsecond){
return false;
}
for(int i=2;i<word.size();i++){
bool tagcurrent=isupper(word[i]);
if(tagcurrent!=tagsecond){
return false;
}
}
return true;
}
};
方法二:
利用位置:
class Solution {
public:
bool detectCapitalUse(string word) {
int up=0,low=0;
for(auto i:word){
if(isupper(i)){
up++;
}
else{
low++;
}
}
return(up==0||low==0||(up==1&&isupper(word[0])));
}
};