520. Detect Capital

来源:互联网 发布:淘宝店铺地址在哪里看 编辑:程序博客网 时间:2024/06/10 02:22

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

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

其实确实不难,就是我的解法实在是太繁琐了,经过师姐提点才知道原来这样解

判断首字母,大写的话,再判断第二个字母,大写的话后面出现小写字母就false

                                                                                 小写的话后面出现大写字母就false

                          小写的话,后面出现大写字母就false

感谢师父看了100行我之前恶习的代码,虽然我俩都觉得逻辑没问题,但就不知道错在哪里了,,,,,,

看了《志明与春娇》和《志明与春娇2》 现在上映的为什么不叫《志明与春娇3》

bool detectCapitalUse(char* word) {    int i=0;    int len=strlen(word);    if(('A'<=word[0])&&(word[0]<='Z')){        if(('A'<=word[1])&&(word[1]<='Z')){            for(i=2;i<len;i++){                if(('a'<=word[i])&&(word[i]<='z')){                    return false;                }            }        }        else{            for(i=2;i<len;i++){                if(('A'<=word[i])&&(word[i]<='Z')){                    return false;                }            }                  }         return true;    }else{        for(i=1;i<len;i++){            if(('A'<=word[i])&&(word[i]<='Z')){                return false;            }        }        return true;    }    }


0 0