LeetCode——Detect Capital
LeetCode——Detect Capital
Question
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.
Answer
class Solution {
public:
bool detectCapitalUse(string word) {
if (word.length() == 1)
return true;
char c = word[0];
if (c >= 'a' && c <= 'z') {
for (int i = 1; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
return true;
} else {
c = word[1];
if (c >= 'A' && c <= 'Z') {
for (int i = 2; i < word.length(); i++) {
if (word[i] < 'A' || word[i] > 'Z')
return false;
}
return true;
} else {
for (int i = 2; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
return true;
}
}
}
};
LeetCode——Detect Capital的更多相关文章
- [LeetCode] 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 Detect Capital
1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...
- 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 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- LeetCode - 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 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- [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 ...
随机推荐
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- 本地代码推送到github仓库
git 初始化 cd 到需要提交的项目目录下,执行git init 配置用户名和邮箱 git config --global user.name "codingID" git co ...
- 巨蟒python全栈开发数据库前端3:CSS基础2
1.文本属性 2.背景属性 3.边框属性 4.display属性 5.盒子模型
- <2014 04 26> 《Coders at Work编程人生:15位软件先驱访谈录》
什么是老派程序员?调试只用printf,关心数据结构,先整体或先局部,不知道OO.IDE.TDD.BDD等等为何物.Ken Thompson,Jamie Zawinski,Joe Armstrong, ...
- Request.RawUrl、Request.Url的区别
如果访问的地址是: http://hovertree.com/guestbook/addmessage.aspx?key=hovertree%3C&n=myslider#zonemenu 那么 ...
- python中open函数的使用
转自:https://www.cnblogs.com/R-ling/p/8412578.html 一.open()的函数原型open(file, mode=‘r', buffering=-1, enc ...
- elastic search 查询语句
部署了半个月,分析一下数据: 需要提前知道的是,tpot中,每天的数据存一个index,然后每个index里面有不同的type,每条请求一个document 共24万条请求: 查看整个集群所有数据 以 ...
- 使用idea的条件断点快速定位注解的处理类
看代码时会碰到注解的处理类难定位的情况,比如spring的某个注解我们想知道到底是谁在处理他,他起什么作用,通过普通的代码搜索功能不容易找到,比如好用的方法就是条件断点. 比如下断:Accessibl ...
- 使用from __future__ import unicode_literals时要注意的问题
add by zhj: 在Python中有些库的接口要求参数必须是str类型字符串,有些接口要求参数必须是unicode类型字符串.对于str类型的字符串,调用len()和遍历时,其实都是以字节为单位 ...
- 这些Python骚操作,你知道吗?
0x00 世界,你好 程序员第一次接触语言或者框架,基本上都有个 Hello World 的例子,这里 Python 直接将它做成了一个包. 0x01 Python 哲学 Python 执行 i ...