【字符串】520. 检测大写字母

题目:

解答:

其实只要记录有多少个大写字母即可,在遍历过程中,如果大写字母的个数小于正在遍历的下标,说明不符合题解,既不是连续的出现大写字母,如 “AaAa” 遍历到第二个 A 时的情况。

最终判断是否为全大写或只是首字母大写即可。

 1 class Solution {
 2 public:
 3     bool detectCapitalUse(string word) 
 4     {
 5         int uc = 0;
 6         for (int i = 0; i < word.size(); i++) 
 7         {
 8             if (isupper(word[i]) && uc++ < i) 
 9             {
10                 return false;
11             }
12         }
13         
14         return uc == word.size() || uc <= 1;
15 
16     }
17 };

猜你喜欢

转载自www.cnblogs.com/ocpc/p/12823187.html