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:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. 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 检测大写格式的更多相关文章

  1. 520 Detect Capital 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的:    全部字母都是大写,比如"USA".    单词中所有字母都不是大写,比如&q ...

  2. Leetcode 520. Detect Capital 发现大写词 (字符串)

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

  3. LeetCode——Detect Capital

    LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...

  4. leetCode Detect Capital

    1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...

  5. 力扣(LeetCode)520. 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

  6. Leetcode520Detect Capital检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

  7. 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 ...

  8. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  9. Java实现 LeetCode 520 检测大写字母

    520. 检测大写字母 给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是 ...

随机推荐

  1. Maven学习笔记一

    maven是apache下的一个开源项目,是纯java开发,并且只是用来管理java项目的. Maven好处 1.普通的传统项目,包含jar包,占用空间很大.而Maven项目不包含jar包,所以占用空 ...

  2. Maven学习笔记二

    依赖范围 <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api&l ...

  3. 敏捷冲刺每日报告——Day3

    1.情况简述 Alpha阶段第一次Scrum Meeting 敏捷开发起止时间 2017.10.27 00:00 -- 2017.10.28 00:00 讨论时间地点 2017.10.27晚9:30, ...

  4. win7如何以管理员身份运行命令提示符(cmd)

    1.进入到: C:\Windows\System32 2.找到cmd.exe文件 3.右键单击 ,选择 以管理员身份运行.

  5. 201621123068 Week04-面向对象设计与继承

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:继承.多态.重载.关键字.父类与子类 1.2 尝试使用思维导图将这些关键词组织起来. 2. 书面作业 1. 面向对象设计(大 ...

  6. [USACO13JAN] Seating

    https://www.luogu.org/problem/show?pid=3071 题目描述 To earn some extra money, the cows have opened a re ...

  7. 用js 获取url 参数 页面跳转 ? 后的参数

    记得之前在原来的公司写过这个东西,但是还是忘记怎么接住参数了,只知道怎么把id传过去! 问了身边的大佬 他首先推荐了我一个链接是别人写好的方法 附上链接地址:http://blog.csdn.net/ ...

  8. 13-TypeScript单例模式

    在JavaScript中,要实现设计模式比较复杂.而在TypeScript中因为使用面向对象的思想编程,要实现设计模式的方式与后端语言C#.Java等非常类似. 单例模式是一种常用的设计模式,通常用于 ...

  9. python 关键字的操作

    声明:本文章默认使用的是python 3.6.1 1.要想当个牛逼的程序员,就要精通各种hello world的写法,当然,我不牛逼,只能用python去写^..^! print("Hell ...

  10. js 中bind

    function fn(a){ this.innerHTML = a; console.log(this); } //fn("hello"); span1.onclick =fun ...