1. 程式人生 > >LeetCode 520:Detect Capital (c++)

LeetCode 520:Detect Capital (c++)

一:題目

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;  
    }