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. [css 揭秘]:CSS编码技巧

    CSS编码技巧 我的github地址:https://github.com/FannieGirl/ifannie 喜欢的给我一个星吧 尽量减少代码重复 尽量减少改动时需要编辑的地方 当某些值相互依赖时 ...

  2. [15单片机] STC15F104W开发入门及模拟串口程序

    STC15F104W开发入门及模拟串口程序 Saturday, 31. March 2018 09:42AM - beautifulzzzz 前言 最近找到一款51内核的SOP8封装的8脚单片机STC ...

  3. Jupyter Notebook的快捷键

    Jupyter Notebook 有两种键盘输入模式. 编辑模式,允许你往单元中键入代码或文本,这时的单元框线是绿色的. 命令模式,键盘输入运行程序命令:这时的单元框线是蓝色.       命令模式 ...

  4. Git忽略规则.gitignore梳理

    对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...

  5. 从零部署Spring boot项目到云服务器(正式部署)

    上一篇文章总结了在Linux云服务器上部署Spring Boot项目的准备过程,包括环境的安装配置,项目的打包上传等. 链接在这里:http://www.cnblogs.com/Lovebugs/p/ ...

  6. 贯穿程序员一生的必备开发技能——debug

    1.什么是debug debug是一种运行模式,用来跟踪程序的走向,以及跟踪程序运行过程中参数的值的变化. 2.debug的作用 debug一般用来跟踪代码的运行过程,通常在程序运行结果不符合预期或者 ...

  7. python 面向对象的程序设计

    一:什么是编程范式? 编程是程序员用特定的语法 + 数据结构 + 算法组成的代码来告诉计算机如何执行任务的过程. 如果把编程的过程比喻为练习武功,那么编程范式指的就是武林中的各种流派,而在编程的世界里 ...

  8. Beta 集合

    Beta冲刺序列: Beta凡事预则立 :Beta No.0 Beta冲刺Day1:Beta No.1 Beta冲刺Day2:Beta No.2 Beta冲刺Day3:Beta No.3 Beta冲刺 ...

  9. C语言第二次作业---分支结构

    一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); } ...

  10. 我所知道的window.location

    多说无益 直接上干货 假如一个地址为  http://127.0.0.1:5000/index.html?id=4 window.location.href -- 完整路径 -- http://127 ...