美文网首页
Leetcode520. 检测大写字母

Leetcode520. 检测大写字母

作者: LonnieQ | 来源:发表于2019-11-15 00:12 被阅读0次

    题目

    给定一个单词,你需要判断单词的大写使用是否正确。

    我们定义,在以下情况时,单词的大写用法是正确的:

    全部字母都是大写,比如"USA"。
    单词中所有字母都不是大写,比如"leetcode"。
    如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。
    否则,我们定义这个单词没有正确使用大写字母。

    示例 1:

    输入: "USA"
    输出: True
    

    示例 2:

    输入: "FlaG"
    输出: False
    

    注意: 输入是由大写和小写拉丁字母组成的非空单词。

    C++代码

    #include <iostream>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    class Solution {
    public:
        bool detectCapitalUse(string word) {
            int numberOfUpperCased = 0;
            for (int i = 0; i < word.size(); ++i) {
                if (word[i] >= 'A' && word[i] <= 'Z') ++numberOfUpperCased;
            }
            return numberOfUpperCased == 0 || numberOfUpperCased == word.size() || (numberOfUpperCased == 1 && isupper(word[0]));
        }
    };
    int main(int argc, const char * argv[]) {
        // insert code here...
        Solution solution;
        cout << solution.detectCapitalUse("USA") << endl;
        cout << solution.detectCapitalUse("FlaG") << endl;
        return 0;
    }
    

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/detect-capital
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    相关文章

      网友评论

          本文标题:Leetcode520. 检测大写字母

          本文链接:https://www.haomeiwen.com/subject/gjxjictx.html