1、简单做法,将三种情况分别列出来。
class Solution {
public:
bool detectCapitalUse(string word) {
int i=1;
if(word[i]=='\0') return true;
else if(word[0]<=90&&word[0]>=65&&word[i]<=122&&word[i]>=97)
{
while(word[i]!='\0')
{
if(word[i]<=90&&word[i]>=65) return false;
++i;
}
return true;
}
else if(word[0]<=90&&word[0]>=65)
{
i=1;
while(word[i]!='\0')
{
if(word[i]<=122&&word[i]>=97) return false;
++i;
}
return true;
}
else
{
i=0;
while(word[i]!='\0')
{
if(word[i]<=90&&word[i]>=65) return false;
++i;
}
return true;
}
}
};