[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 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.
这道题给了我们一个单词,让我们检测大写格式是否正确,规定了三种正确方式,要么都是大写或小写,要么首字母大写,其他情况都不正确。那么我们要做的就是统计出单词中所有大写字母的个数cnt,再来判断是否属于这三种情况,如果cnt为0,说明都是小写,正确;如果cnt和单词长度相等,说明都是大写,正确;如果cnt为1,且首字母为大写,正确,其他情况均返回false,参见代码如下:
解法一:
class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = , n = word.size();
for (int i = ; i < n; ++i) {
if (word[i] <= 'Z') ++cnt;
}
return cnt == || cnt == n || (cnt == && word[] <= 'Z');
}
};
下面这种方法利用了STL的内置方法count_if,根据条件来计数,这样使得code非常简洁,两行就搞定了,丧心病狂啊~
解法二:
class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = count_if(word.begin(), word.end(), [](char c){return c <= 'Z';});
return cnt == || cnt == word.size() || (cnt == && word[] <= 'Z');
}
};
参考资料:
https://discuss.leetcode.com/topic/79912/3-lines
https://discuss.leetcode.com/topic/79930/java-1-liner
https://discuss.leetcode.com/topic/80314/6ms-2-lines-c-solution/2
https://discuss.leetcode.com/topic/79911/simple-java-solution-o-n-time-o-1-space
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Detect Capital 检测大写格式的更多相关文章
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- LeetCode——Detect Capital
LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...
- leetCode Detect Capital
1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...
- 力扣(LeetCode)520. 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...
- Leetcode520Detect Capital检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...
- 50. leetcode 520. Detect Capital
520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- Java实现 LeetCode 520 检测大写字母
520. 检测大写字母 给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是 ...
随机推荐
- jmeter常见问题汇总
Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为"聚合报告".今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下, ...
- 用SQL语言操作数据
使用T-SQL插入数据(一)SQL是什么?Structured Query Language :结构化查询语言T-SQL:Transact-SQLT-SQL是SQL的增强版对功能进行了扩充 ...
- 使用ADO.NET查询和操作数据库
String和StringBuilder 语法: //声明一个空的StringBuilder对象 StingBuilder对象名称 = new StringBuilder(); //声明一个Str ...
- Object类-----java
Object类是所有类的父类,如果一个类没有使用extends关键字明确标识继承另一个类,那么这类默认继承object类 Object类中的方法,适合所有子类. 1 toString()方法在Obje ...
- C语言第一次博客作业 陈张鑫
一,PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- 20155306 2017-2018-1《信息安全系统设计》第二周课堂测试以及myod的实现
20155306 2017-2018-1<信息安全系统设计>第二周课堂测试以及myod的实现 第二周课堂测验: (注:前两项在课堂已提交,在此不做详解) 第一项: 每个.c一个文件,每个. ...
- Beta版本敏捷冲刺每日报告——Day1
1.情况简述 Beta阶段第一次Scrum Meeting 敏捷开发起止时间 2017.11.2 08:00 -- 2017.11.2 21:00 讨论时间地点 2017.11.2晚6:00,软工所实 ...
- tornado options
tornado.options.define() 用来定义options选项变量的方法,定义的变量可以在全局的tornado.options.options中获取使用,传入参数: name 选项变量名 ...
- BM V7000数据恢复成功案例;服务器数据恢复
IBM V7000存储是一款定位中端的存储设备,很多企业选择该服务器作为存储,最近北亚数据恢复中心接到一例V7000服务器数据恢复案例,下面将对本次数据恢复的过程和数据恢复方法进行归纳总结,希望对各位 ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...