LeetCode - 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.


思路與步驟:

我主要想到兩種思路:

1. 先判斷第一個字母,如果爲大寫,則需要後面全爲大寫或者全爲小寫;如果是小寫,則需要後面全爲小寫;

2. 先判斷從第二個開始的所有字母,如果全爲小寫,則需要第一個是字母即可;如果全爲大寫,則需要第一個也是大寫。

其他情況全返回 false。

效率還可以,但是代碼看上去不夠簡潔,而且判斷循環較多,後面學習了別人的代碼,得到了一些新的思路

別人的思路:

這裏只說返回 true 的情況

思路1

先計算一下word 中大寫字母的數量,如果爲0個大寫或者全爲大寫,那麼返回 true;如果只有一個大寫,那麼這個大寫字母必須是第一個字母。也就這三種。然後要做的就是怎樣寫出漂亮的代碼了。尤其要學習那些寫法 ,很簡潔。(代碼AC了,但是有些情況沒有考慮到,例如第一位是大寫字母,但是後面的可能不是字母;大寫字母個數爲0,但是不是全爲小寫,而是有其他字符等)

思路2

比較原來的 word 是否與大寫後的相等,如果不相等,就看其從第2個字符開始到最後的子串是否與小寫後相等。主要用到幾個方法 equals(); word.toUpperCase(); substring() (代碼AC了,但是有些情況沒有考慮到,例如第一位不是字母后面全爲小寫也可以通過)

思路3

正則表達式

通過學習別人的方法,發現本題有個隱藏的條件,就是默認輸入的字符串只含有大小寫字母,而不包含其他字符。但是在測試的時候可以用保護非字母的字符進行測試,並返回false。

編程實現:

思路1:

public class Solution {
    public boolean detectCapitalUse(String s) {
        if(s.length() <= 1) return true;
        int i = 1;
        if(Character.isLowerCase(s.charAt(0)))
            while(i<s.length() && Character.isLowerCase(s.charAt(i)))   i++;
        else if(Character.isUpperCase(s.charAt(0)) && Character.isLowerCase(s.charAt(1)))
            while(i<s.length() && Character.isLowerCase(s.charAt(i)))   i++;
        else if(Character.isUpperCase(s.charAt(0)))
            while(i<s.length() && Character.isUpperCase(s.charAt(i)))   i++;
        if(i==s.length()) return true;
        else return false;
    }
}

思路2:

public class Solution {
    public boolean detectCapitalUse(String s) {
        if(s.length() == 0 || s.length() == 1) return true;
        int i = 1;
        for(i=1; i<s.length() && 'a'<=s.charAt(i) && s.charAt(i)<='z'; i++);
        if(i==s.length() && Character.isLetter(s.charAt(0)))    return true;
        for(i=1; i<s.length() && 'A'<=s.charAt(i) && s.charAt(i)<='Z'; i++);
        if(i==s.length() && 'A'<=s.charAt(0) && s.charAt(0)<='Z')    return true;
        return false;
    }
}

別人的思路1:

public class Solution {
    public boolean detectCapitalUse(String word) {
        int numUpper = 0;
        for (int i=0;i<word.length();i++)   if (Character.isUpperCase(word.charAt(i)))  numUpper++;
        if (numUpper == 0 || numUpper == word.length()) return true;
        if (numUpper == 1) return Character.isUpperCase(word.charAt(0));
        return false;
        //return (numUpper==0 || numUpper==word.length() || (numUpper==1 && Character.isUpperCase(word.charAt(0))));
    }
}
期中最後3行可以用註釋掉的那一行代替。


別人的思路2:

public class Solution {
    public boolean detectCapitalUse(String word) {
        return word.equals(word.toUpperCase()) || word.substring(1).equals(word.substring(1).toLowerCase()));
    }
}

 別人的思路3:

public class Solution {
    public boolean detectCapitalUse(String word) {
        return word.matches("[A-Z]+|[a-z]+|[A-Z][a-z]+");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章