LeetCode 520. Detect Capital 自己的解法

题目

Given a word, you need to judge whether the usage of capitals in it is right or not.
大意:给定一个单词,判断大写用法是否正确

如何判断

三种情况:每个字符全是大写,每个字符全是小写,除了首字符大写其它字符全小写

解法

也就是根据以上三种情况判断

class Solution {
    public boolean detectCapitalUse(String word) {
        if(word.toUpperCase().equals(word))
            return true;
        if(word.charAt(0)>='A'&&word.charAt(0)<='Z'&&(word.substring(1).toLowerCase().equals(word.substring(1))))
            return true;
        if(word.toLowerCase().equals(word))
            return true;
        return false;

    }
}

猜你喜欢

转载自blog.csdn.net/ch_609583349/article/details/77745644