当前位置:网站首页>520. detect capital letters

520. detect capital letters

2022-06-24 08:52:00 Drag is me

leetcode Force button to brush questions and punch in

subject :520. Detect capital letters
describe : We define , When , The capitalization of words is correct :

All the letters are uppercase , such as “USA” .
All the letters in a word are not capitalized , such as “leetcode” .
If a word contains more than one letter , Only the first letter is capitalized , such as “Google” .
Give you a string word . If capitalized correctly , return true ; otherwise , return false .

Their thinking

1、 Sequential programming ;
2、 First determine whether the first letter is upper case or lower case , If word[0] It's lowercase , Then the following must be lowercase to return true;
3、 If word[0] It's capital , There are two situations , see word[1] Uppercase or lowercase . If word[1] It's capital , The following must also be capitalized to return true; If word[1] It's lowercase , The following must all be lowercase to return true;

Source code ##

class Solution {
    
public:
    bool detectCapitalUse(string word) {
    
        int len = word.size();
        if (len == 1) return true;
        if (word[0] >= 'a' && word[0] <= 'z') {
    
            for (int i = 1; i < word.size(); ++i) {
    
                if (word[i] >= 'A' && word[i] <= 'Z') return false;
            }
            return true;
        } else {
    
            if (word[1] >= 'A' && word[1] <= 'Z') {
    
                for (int i = 1; i < word.size(); ++i) {
    
                    if (word[i] >= 'a' && word[i] <= 'z') return false;
                }
                return true;
            } else {
    
                for (int i = 1; i < word.size(); ++i) {
    
                    if (word[i] >= 'A' && word[i] <= 'Z') return false;
                }
                return true;
            }
            
        }
        return true;   
    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240627023061.html