DetectCapitalOPT [source code]

public class DetectCapitalOPT {
    public boolean detectCapitalUse(String word) {
        if (word.length() == 1) return true;

        if (word.charAt(0) >= 65 && word.charAt(0) <= 90) {
            if (word.charAt(1) >= 65 && word.charAt(1) <= 90) {
                for (int i = 2; i < word.length(); i++) {
                    if (!(word.charAt(i) >= 65 && word.charAt(i) <= 90)) return false;
                }
            } else for (int i = 2; i < word.length(); i++) {
                if (!(word.charAt(i) >= 97 && word.charAt(i) <= 122)) return false;
            }
        } else for (int i = 1; i < word.length(); i++) {
            if (!(word.charAt(i) >= 97 && word.charAt(i) <= 122)) return false;
        }

        return true;
    }

    public static void main(String[] args) {
        DetectCapitalOPT tester = new DetectCapitalOPT();
        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";
    }
}

这个是最优解, 25ms;

这里这个代码跟我自己写的版本都展示了一个共同的问题, 就是当有多个 if 嵌套的时候, 最好不要随便省略括号. 不然后面跟进的 else 很容易无法分清到底是跟哪个 if 配对;

大概看了一下这个算法, 跟我的算法基本没有什么区别, 基本就是基于对于0,1两位的判断, 然后后面扫一遍拉倒.

未必不同的一个地方在于他把所有的return true都集合在一起了.

这个思想其实是我一直想要总结的.
用consecutive if, 或者nested if else结构的时候, 一个理想的状态是, 尤其是在返回值只有两个选择的时候, 最好能够把对其中一个选择的 exit 全部统一到一个地方;

另外一种比较简单的思路就是, 你看我自己的这个代码:

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

这个很容易看到的一个是, return true其实出现了三次, 所以很直觉的就可以 refactor, 把return true给合并到一个地方;

不过以后如果还是碰到binary return value的情况, 是可以考虑, 在各种nested if else OR consective if的过程当中, 只追求寻找某一个返回值的判断, 然后把另外一个返回值的情况全部合并到最后一个default return. 这个思路可以取名为default return;

对于返回值的 state 数量多于两个的情况, 在有些特殊情况下估计还是可以进行default return的操作, 不过具体情况可能相对就复杂很多;

其实以前一直在说的:

for (...) {  
    if (...) return ...;  
}  
return ...;

这个就是default return的最简单的应用情形, 当时理解的时候的侧重点放在了loop内部的 return, 称之为premature exit, 现在看来更广义的来说, 应该理解为 loop, 或者 conditional 里面只追去判断only one of the binary states;


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 ""