leetcode解题报告(30):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
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;
}
};
日常水题。。。
leetcode解题报告(30):Detect Capital的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- [LeetCode解题报告] 502. IPO
题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...
- [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 u ...
- leetCode解题报告5道题(九)
题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- PB 奇葩BUG
1. 在数据窗口的Edit 中选则CHeckBox 后,必须给默认值,否则会导致数据保存不了,也无提示. 2.当使用数据窗口的 setfile()函数时 如果 条件中有两个LIKE 则会报错,一个LI ...
- C# vb .net实现灰度化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的灰度化呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步: ...
- Ruby开发小记
基础点 1.log打印 puts "Hello!" 2.拼接字符 value1 = "today" value2 = "#{value1} is Th ...
- window 命令行强制删除文件、文件夹
1. 强制删除文件文件夹和文件夹内所有文件 rd/s/q D:\app 2. 强制删除文件,文件名必须加文件后缀名 del/f/s/q D:\app.txt
- JAVA基础之ServletContext应用
创建一个登陆的界面,并且统计次数! 导入jar包; 1. driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java0603?u ...
- SVN上文件出现左侧黄色箭头右侧绿色箭头的双向箭头
转自:https://blog.csdn.net/jiuweihu521/article/details/90902152 与资源库对比又没有要提交的东西,网上说删除这个目录,然后更新整个配置库..我 ...
- iOS 关键词assign、strong、copy、weak、unsafe_unretained
关键词assign.strong.copy.weak.unsafe_unretained 影响: 是否开辟新的内存 是否有引用计数增加 strong 指向并拥有该对象.其修饰的对象引用计数会 +1,该 ...
- 如何在Hybris commerce里创建一个media对象
进入backoffice的Media中心, 首先新建一个文件夹,用于存放即将创建的media对象: 取名为jerryimage: 然后创建一个新的media对象,取名jerryproductimage ...
- 安装node.js->npm->vue
我们研究vue时,首先操作的就是vue的引用,大部分人为了方便直接在页面上引用vue.js,但是一些大型网站还是比较喜欢用vue的npm命令来安装vue并使用,之前研究vue时,研究过使用npm安装的 ...
- mysql基础知识整理(一)
一.数据库基本操作 登录: 开启数据库服务,在cmd中输入指令 mysql -u用户名 -p密码 3退出: 在cmd中输入exit/quit;启动服务: net start 服务名 停止服务:net ...