leetCode刷题记录68_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:
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
判断给定字符串的大写字母使用是否规范.
/*****************************************************我的解答*************************************************
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    var condition1 = function(word){
        var upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for(var index = 0; index < word.length; index++)
        {
            if(upperLetters.indexOf(word.charAt(index)) === -1)
            {
                return false;
            }    
        }    
        return true;
    };
    var condition2 = function(word){
        var letters = 'abcdefghijklmnopqrstuvwxyz';
        for(var index = 0; index < word.length; index++)
        {
            if(letters.indexOf(word.charAt(index)) === -1)
            {
                return false;
            }
        }
        return true;
    };
    var condition3 = function(word){
        var upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var letters = 'abcdefghijklmnopqrstuvwxyz';
        var con1 = false;
        var con2 = true;
        if(upperLetters.indexOf(word.charAt(0)) != -1)
        {
            con1 = true;
        }    
        for(var index = 1; index < word.length; index++)
        {
            if(upperLetters.indexOf(word.charAt(index)) != -1)
            {
                con2 = false;
                break;
            }    
        }    
        return con1 && con2;
    };
    return condition1(word) || condition2(word) || condition3(word);
};

发布了135 篇原创文章 · 获赞 10 · 访问量 6255

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/89010865