leetcode (Detect Capital)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85062387

Title:Detect Capital     520

Difficulty:Easy

原题leetcode地址:https://leetcode.com/problems/detect-capital/

1.    一一判断

时间复杂度:O(1),但是调用String的equals()和substring()方法。

空间复杂度:O(1),没有申请额外空间。

    /**
     * 一一判断
     * @param word
     * @return
     */
    public static boolean detectCapitalUse(String word) {

        // 长度小于2,只有一个字符,不关心大小写,都返回true
        if (word.length() < 2) {
            return true;
        }

        // 判断所有字符是否都是大小
        if (word.toUpperCase().equals(word)) {
            return true;
        }

        // 判断除第一个字符外的字符是不是都是小写
        if (word.substring(1).toLowerCase().equals(word.substring(1))) {
            return true;
        }

        return false;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85062387