1. 程式人生 > >LeetCode 520. Detect Capital

LeetCode 520. Detect Capital

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

題目大意:判斷一個字母是否大小寫正確:要麼全是大寫,要麼全是小寫,或者首字母大寫其他小寫,否則不滿足題意~
分析:判斷word[0]和word[1]的大小寫,如果word[0]是小寫,那後面必須是小寫,如果word[0]是大寫word[1]是小寫,那後面也必須是小寫,如果word[0]是大寫word[1]也是大寫那麼後面必須都是大寫~

class Solution {
public:
    bool detectCapitalUse(string word) {
        if (word.length() <= 1) return true;
        if (islower(word[0]) || (isupper(word[0]) && islower(word[1]))) {
            for (int i = 1; i < word.length(); i++)
                if (isupper(word[i])) return false;
        } else {
            for (int i = 1; i < word.length(); i++)
                if (islower(word[i])) return false;
        }
        return true;
    }
};

相關推薦

LeetCode - 520. Detect Capital

style detect inpu lower fine whether span string har Given a word, you need to judge whether the usage of capitals in it is right or not.

leetcode-520-Detect Capital

ons emp ctc con miss 很慢 字符 other 都是 題目描述: Given a word, you need to judge whether the usage of capitals in it is right or not. We defin

LeetCode-520. Detect Capital

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

LeetCode 520. Detect Capital

520. Detect CapitalGiven a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals

[LeetCode&Python] Problem 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

LeetCode之路:520. Detect Capital

一、引言 這道題有關於處理字元的大小寫問題,對於熟悉字元的大小寫處理函式非常有幫助。 這裡粘出題目資訊: Given a word, you need to judge whether the usage of capitals in it is r

LeetCode520 Detect Capital

題目大意 判斷一個單詞的大小寫運用是否正確。全部大寫、全部小寫和首字母大寫是正確的寫法。 解法一 分三種情況: 首字母小寫,如果之後有一個大寫字母,則返回false。 首字母大寫,

Leetcode PHP題解--D81 520. Detect Capital

D81 520. Detect Capital 題目連結 520. Detect Capital 題目分析 給定一個單詞,判斷其使

520. Detect Capital(python+cpp)

題目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define t

leetcodeDetect Capital

Title:Detect Capital     520 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/detect-capital/   1.  

520. Detect Capital

分析 簡單的字串合法性檢測,只能是首字母大寫或全字母大寫。 bool detectCapitalUse(char* word) { int wordLen = strlen(wo

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

LeetCode 520Detect Capital (c++)

一:題目 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

LeetCode: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 righ

字符串(1)——Detect Capital

use pub let 求解 owin force bsp cas 升級版 Given a word, you need to judge whether the usage of capitals in it is right or not. We define the

Detect Capital

大寫 inpu 三種 example return empty other tco pty Given a word, you need to judge whether the usage of capitals in it is right or not. We def

Leetcode 520

給定一個單詞,你需要判斷單詞的大寫使用是否正確。 我們定義,在以下情況時,單詞的大寫用法是正確的: 全部字母都是大寫,比如"USA"。 單詞中所有字母都不是大寫,比如"leetcode"。 如果單詞不只含有一個字母,只有首字母大寫, 比如 "Google

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

LeetCode520. 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 r