DetectCapital [source code]

public class DetectCapital {
    public boolean detectCapitalUse(String word) {
        char[] letters = word.toCharArray();
        if (letters.length <= 1) return true;
        boolean res = false;
        if (!isCapital(letters[0])) {
            for (int i = 1; i < letters.length; i++) if (isCapital(letters[i])) return false;
            return true;
        }  // do not delete this pair of braces, otherwise would be ambiguity of parsing
        else {
            if (!isCapital(letters[1])) {
                for (int i = 2; i < letters.length; i++) if (isCapital(letters[i])) return false;
                return true;
            } else {
                for (int i = 2; i < letters.length; i++) if (!isCapital(letters[i])) return false;
                return true;
            }
        }
    }

    public boolean isCapital(char c) {
        return c >= 65 && c <=90;
    }

    public static void main(String[] args) {
        DetectCapital tester = new DetectCapital();
        String input1 = "Google";
        assert tester.detectCapitalUse(input1) == true : "fail 1";
        String input2 = "GooGle";
        assert tester.detectCapitalUse(input2) == false : "fail 2";
        String input3 = "KDJLSJ";
        assert tester.detectCapitalUse(input3) == true : "fail 3";
        String input4 = "dlkajfdl";
        assert tester.detectCapitalUse(input4) == true : "fail 4";
        String input5 = "adaDFda";
        assert tester.detectCapitalUse(input5) == false : "fail 5";
        String input6 = "G";
        assert tester.detectCapitalUse(input6) == true : "fail 6";
    }
}

题目的 note 里面有一个will这个词, 所以你可以默认所有的 input 只有字母, 而不用进行主观的判断;

大小写问题其实已经碰到过好几次了, 对于大写字母的范围和小写字母的 ASICII 码范围现在一定要很熟悉:

java> (int)'a'  
java.lang.Integer res0 = 97  
java> (int)'z'  
java.lang.Integer res1 = 122  
java> (int)'A'  
java.lang.Integer res2 = 65  
java> (int)'Z'  
java.lang.Integer res3 = 90

String 需要遍历的时候, 先转换成 Char Array 很多时候是一个好主意;

else后面的部分一开始是这么写的:

        else for (int i = 1; i < letters.length; i++) res ^= isCapital(letters[i]);  
        return !res;

这个写法是不对的, 这里是把 xor 当成 lor 或者 land 来用了, 要知道, false ^ true ^ true ^ ... ^ true
是未必得到 true 的;

总结一下, 这个问题总体来说还是一个很常规的 stream 类问题, 只要处理好各种逻辑分叉就行了.
第一次 fail 是忘记考虑"G" 这样的 case 了. 这个确实有点隐藏性.

为了避免这种情况, 比如这个题目, 你感觉大体差不多准备提交之前, 开始自己尝试找这种最常规的boundary case;
比如empty, 或者这里的, 一个字母的之类的. 不够这里这个"G" 确实不是特别容易想到;

速度是32ms, 62%. 不算特别优秀, 还是可以改进;


Problem Description

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:

All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
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.

Subscribe to see which companies asked this question.

Hide Tags String

results matching ""

    No results matching ""