0
点赞
收藏
分享

微信扫一扫

【快乐水题】520. 检测大写字母


原题:

​​力扣链接:520. 检测大写字母

题目简述:

我们定义,在以下情况时,单词的大写用法是正确的:

全部字母都是大写,比如 “USA” 。
单词中所有字母都不是大写,比如 “leetcode” 。
如果单词不只含有一个字母,只有首字母大写, 比如 “Google” 。
给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。

解题思路

1.处理只有一个大写字母的情况;
2.检测全是大写字母的情况;
3.检测全是小写字母的情况;
4.检测首字母是大写,其他都是小写的情况;
5.over;

C++代码:

class Solution {
public:
bool detectCapitalUse(string word) {
int n = word.length();

if(n == 1 && checkBig(word[0]))
{
return true;
}

int i = 0;
bool bRet1 = false, bRet2 = false, bRet3 = false;

///< 1
for(i = 0; i < n; i++)
{
// cout << word[i] << endl;

if(!checkBig(word[i]))
{
break;
}
}
if(i == n)
{
bRet1 = true;
cout << "bRet1" << endl;
}

///< 2
for(i = 0; i < n; i++)
{
if(checkBig(word[i]))
{
break;
}
}
if(i == n)
{
bRet2 = true;
cout << "bRet2" << endl;
}

///< 3
i = 0;
if(checkBig(word[0]) && n > 1)
{
for(i = 1; i < n; i++)
{
if(checkBig(word[i]))
{
break;
}
}

if(i == n)
{
bRet3 = true;
cout << "bRet3" << endl;
}
}

return bRet1 || bRet2 || bRet3;
}

bool checkBig(char str)
{
//cout << str << endl;

if(str >= 'A' && str <= 'Z')
{
//cout << "true" << endl;
return true;
}

//cout << "false" << endl;
return false;
}
};

力扣结果展示:

【快乐水题】520. 检测大写字母_字符串


举报

相关推荐

0 条评论