Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 520. Detect Capital

Rain Hu

520. Detect Capital


一、題目

We define the usage of capitals in a word to be right when one of the following cases holds:

Example 1:

Example 2:

Constraints:


二、分析

三、解題

1. String

bool detectCapitalUse(string word) {
    if (word.length() == 1) return true;
    if (word[0] >= 'a') {                   // 小寫
        for (int i = 1; i < word.length(); i++){
            char& c = word[i];
            if (c < 'a') return false;
        } 
    } else {                                // 大寫
        if (word[1] < 'a') {                // 大寫+大寫
            for (int i = 2; i < word.length(); i++) {
                char& c = word[i];
                if (c >= 'a') return false;
            }
        } else {                            // 大寫+小寫
            for (int i = 2; i < word.length(); i++) {
                char& c = word[i];
                if (c < 'a') return false;
            }
        }
    }
    return true;
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 2523. Closest Prime Numbers in Range
Next
[LeetCode] 2521. Distinct Prime Factors of Product of Array