leetcode解题报告(30):Detect Capital
2024-10-21 05:51:57
描述
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
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
分析
每遇到一个大写字母,就将变量upCount加1,遍历结束后,如果要返回真,那么会满足下列条件之一:
- upCount == word.size()。即所有字母均为大写
- upCount == 0.即所有字母均为小写
- upCount == 1.有且仅有第一个字母为大写。
否则,返回假。
代码如下:
class Solution {
public:
bool detectCapitalUse(string word) {
int upCount = 0;
if(word.size() == 0)return false;
for(int i = 0; i != word.size(); ++i)
if(isupper(word[i]))++upCount;
if(upCount == word.size() || upCount == 0 || (upCount == 1 && isupper(word[0])))return true;
return false;
}
};
日常水题。。。
最新文章
- word20161218
- Oracle数据库全球化
- chrome控制台调试学习笔记 暂未整理
- ArcEngine拓扑
- CSS打造经典鼠标触发显示选项
- 添加事件(jquery)
- Java设计模式12:常用设计模式之外观模式(结构型模式)
- 阐述Lambada表达式
- WebBrowser控件使用技巧分享
- robin 今日南
- handler的使用
- Selenium的简单安装和使用
- python链接mysql以及常用语法
- window.location.hash 使用说明
- IScroll5要防止重复加载
- 洛谷P4705 玩游戏 [生成函数,NTT]
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程(转)
- PAT 1085 PAT单位排行(25)(映射、集合训练)
- 谈谈 Python 程序的运行原理
- python中的运算符的分类以及使用方法