国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

520. Detect Capital(C++)

2019-11-06 07:01:51
字體:
來源:轉載
供稿:網友

題目:

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: TrueExample 2:Input: "FlaG"Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

class Solution {public: bool detectCapitalUse(string word) { }};

翻譯

檢測資本 給定一個詞,你需要判斷它中的資本的使用是否正確。

當以下情況之一存在時,我們將單詞中資本的使用定義為正確:

這個單詞中的所有字母都是大寫,如“USA”。 這個單詞中的所有字母都不是大寫,例如“leetcode”。 只有此字中的第一個字母是大寫,如果它有多個字母,如“Google”。 否則,我們定義這個詞不以正確的方式使用大寫。

注意:輸入將是由大寫和小寫拉丁字母組成的非空字。

解析:

這就是一個要求不斷實現幾個要實現的條件的題,重點是要如何去設置條件。

遇到多條件的,我的通常做法是:先把條件列下來,然后看哪些條件可以歸類,成為同一組,或者說用一個表達式表示多個條件,這樣就可以達到間接的目的。

都是大寫都是小寫如果有多個字母,頭字母大寫

合并: 首先判斷是不是一個字母,則不論大小寫都符合, 。。。。。

解答:

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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 和林格尔县| 图片| 安吉县| 西丰县| 措美县| 厦门市| 屏东市| 嘉黎县| 扎兰屯市| 大洼县| 稻城县| 湘潭县| 清丰县| 仁怀市| 盐边县| 屏边| 乐业县| 铁岭县| 永嘉县| 徐汇区| 哈巴河县| 沾益县| 荣成市| 郴州市| 新河县| 上饶市| 红河县| 延庆县| 玉田县| 绥芬河市| 泌阳县| 临洮县| 岫岩| 久治县| 怀仁县| 晋城| 万载县| 晋州市| 海安县| 长乐市| 泰顺县|