【快乐水题】520. 检测大写字母
生活随笔
收集整理的这篇文章主要介绍了
【快乐水题】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;///< 1for(i = 0; i < n; i++){// cout << word[i] << endl;if(!checkBig(word[i])){break;}}if(i == n){bRet1 = true;cout << "bRet1" << endl;}///< 2for(i = 0; i < n; i++){if(checkBig(word[i])){break;}}if(i == n){bRet2 = true;cout << "bRet2" << endl;}///< 3i = 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. 检测大写字母的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 互联网日报 | 阿里国内消费者已接近10
- 下一篇: 这年头「野路子」产品太多了