LeetCode 520:Detect Capital (c++)

来源:互联网 发布:js当前时间减一小时 编辑:程序博客网 时间:2024/05/29 09:12

一:题目

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.

二:解题分析

判断一个单词的字母大写使用是否正确

1.所有字母都大写

2.所有字母都小写

3.首字母大写,其余都小写


4种情况:

1.如果字符串长度小于等于1,返回true;

2.如果第一个位置小写,以后出现大写,返回false

3.如果第一个位置大写,第二个位置大写,以后出现小写,返回false

4.如果第一个位置大写,第二个位置小写,以后出现大写,返回false


三:代码实现

思路一:

class Solution {public:    bool detectCapitalUse(string word) {                if(word.length()<=1)            return true;                    int i;        bool first=false;  //true:第一个位置大写,false:第一个位置小写        bool second=false; //true:第二个位置是大写, false:第二个位置小写        if(word[0]>='A' && word[0]<='Z')            first=true;        if(word[1]>='A' && word[1]<='Z')            second=true;        //如果第一个位置小写,以后出现大写,返回false        if(!first && second)            return false;        for(i=2;i<word.length();i++){            //1.如果第一个位置小写,以后出现大写,返回false            if(!first && !second && word[i]>='A' && word[i]<='Z')                return false;                            //2.如果第一个位置大写,第二个位置大写,以后出现小写,返回false             if(first && second && word[i]>='a' && word[i]<='z')                return false;                            //3.如果第一个位置大写,第二个位置小写,以后出现大写,返回false             if(first && !second && word[i]>='A' && word[i]<='Z')                return false;            }        return true;            }};

思路二:

统计大写字母的个数

1.如果全为大写,最终大写字母的个数应该等于字符串的长度

2.如果首字母大写,其余为小写,则大写字母的个数为1

3.全为小写,大写字母个数为0

class Solution {public:    bool detectCapitalUse(string word) {             int i;        int numOfCapital=0;        for(i=0;i<word.length();i++)            if(word[i]>='A'  && word[i]<='Z')                numOfCapital++;        //全为小写        if(numOfCapital==0)            return true;        //全为大写        if(numOfCapital==word.length())            return true;        //首字母大写,其余小写        if(word[0]>='A'  && word[0]<='Z' && numOfCapital==1)            return true;        return false;            }};

思路三(JAVA):

将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,
否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,
否则返回false。

public boolean detectCapitalUse(String word) {        int len=word.length();        String up = word.toUpperCase();          String low = word.toLowerCase();          if (word.equals(up) || word.equals(low))              return true;           String last = word.substring(1, len);          if (last.toLowerCase().equals(last))             return true;           return false;      }