国产探花免费观看_亚洲丰满少妇自慰呻吟_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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 柳江县| 瑞安市| 逊克县| 江西省| 达州市| 九寨沟县| 贵定县| 惠安县| 遂川县| 嘉黎县| 新乐市| 高密市| 布尔津县| 邵阳市| 无锡市| 改则县| 韩城市| 石景山区| 晋中市| 蓝田县| 哈巴河县| 安塞县| 象山县| 贡嘎县| 都匀市| 南平市| 雷山县| 沂南县| 江山市| 磐安县| 晋江市| 甘孜县| 临猗县| 西安市| 茶陵县| 涿州市| 佳木斯市| 商都县| 峨边| 姜堰市| 海盐县|