Leetcode 520 Detect Capital

来源:互联网 发布:网络上的聊污什么意思 编辑:程序博客网 时间:2024/05/29 00:33

Leetcode 520 Detect Capital

#include <string>using namespace std;class Solution {public:    bool detectCapitalUse(string word) {        if (word.length() <= 1)            return true;        bool sign = true;        if (isupper(word[0])){            int upperCount = 0;            for (int i = 1; i < word.length(); i++){                if (isupper(word[i]))                    upperCount++;            }            if (upperCount != 0 && upperCount != word.length() - 1)                sign = false;        }        else if (islower(word[0])){            for (int i = 0; i < word.length(); i++){                if (isupper(word[i]))                    sign = false;            }        }        return sign;    }};class Solution {public:    bool detectCapitalUse(string word) {        int upperCount =0; //变量一定要初始化        for (int i = 0; i < word.length(); i++){            if (word[i] >= 'A' && word[i] <= 'Z')                upperCount++;        }        if (word[0] >= 'A' && word[0] <= 'Z'){            if (upperCount == 1 || upperCount == word.length())                return true;        }        else if (upperCount == 0)            return true;        return false;    }};//这样比上一种方法要快一倍
原创粉丝点击