520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".

Otherwise, we define that this word doesn't use capitals in a right way.

Example 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

C++(15ms):
 1 class Solution {
 2 public:
 3     bool detectCapitalUse(string word) {
 4          int up_num = 0 ;
 5          for(char c : word){
 6             if(c <= 'Z')
 7                 up_num++ ;
 8          }
 9          if(up_num==word.size() || up_num==0 || (up_num==1&&word[0]<='Z'))
10             return true ;
11          else
12             return false ;
13     }
14 };
原文地址:https://www.cnblogs.com/mengchunchen/p/6550816.html