
class Solution {
public:
bool detectCapitalUse(string word)
{
if(word.size()==1)
return true;
string high,low,other;
for(int i=0;i<word.size();i++)
{
high+=toupper(word[i]);
low+=tolower(word[i]);
if(i==0)
other+=toupper(word[i]);
else
other+=tolower(word[i]);
}
return word==high||word==low||word==other;
}
};
bool detectCapitalUse(string word)
{
if (word.size() < 2) return true;
bool flag_first = isupper(word[0]);
bool flag_second = isupper(word[1]);
if (!flag_first && flag_second) return false;
for (int i = 2; i < word.size(); i++)
{
bool flag_cur = isupper(word[i]);
if (flag_second != flag_cur) return false;
}
return true;
}
本文介绍了一种检测字符串是否正确使用大小写的算法。该算法首先判断字符串长度,然后通过比较字符串与全大写、全小写及首字母大写其余小写的三种情况来判断其是否符合规范。
441

被折叠的 条评论
为什么被折叠?



