LeetCode: 520 Detect Capital(easy)
题目:
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.
代码:
自己的:
class Solution {
public:
bool detectCapitalUse(string word) {
bool result = ;
if (word[] < 'a'){
if (word[] < 'a'){
for(int i = ; i < word.size(); i++){
if(word[i] > 'Z'){
result = ;
break;
}
}
}
else{
for(int i = ; i < word.size(); i++){
if(word[i] < 'a'){
result = ;
break;
}
}
}
}
else{
for(auto c : word){
if (c < 'a'){
result = ;
break;
}
}
}
return result;
}
};
别人的:
class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = ;
for(int a = ; a < word.length(); a++ ) {
if('Z' - word[a] >= )
cnt++;
}
if(cnt == word.length() || cnt == || (cnt == && ('Z' - word[] >= )))
return true;
return false; }
};
对大写字母计数,然后再判断(全是大写、全是小写、首字母大写)
LeetCode: 520 Detect Capital(easy)的更多相关文章
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 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
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 ...
- 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
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- [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】520. Detect Capital 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...
- 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the ...
随机推荐
- 使用mark-sweep算法的垃圾回收器
在我写C++代码的那些时间里,我没有写过垃圾回收器,也没有实现过自己的内存分配器,这方面的文章倒是看了不 少.比如我在写C#代码时只管new而不需要释放,我也明白有个垃圾回收器在那帮我回收那些堆上的对 ...
- javaScript中innerHTML,innerText,outerHTML,outerText的区别
开头说下innerText和outerText只在chrome浏览器中有效 定义和用法 innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML,包括标签. 来看代码 <!DOC ...
- 最新wap手机agent
名称 agent 铃声格式 和弦数 数据量 删除 LGE-CU8080 LGE-CU8080/1.0 UP.Browser/4.1.26l UP.Link/5.1.2.9 pmd2.0 40 ...
- 51 NOD 1753 相似子串 字符串hash
1735 相似子串 基准时间限制:5 秒 空间限制:131072 KB 分值: 80 两个字符串相似定义为:1.两个字符串长度相等2.两个字符串对应位置上有且仅有至多一个位置所对应的字符不 ...
- zoj 2271 Chance to Encounter a Girl <概率DP>
题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2271 题意:一个N*N的矩阵( N = 2*k-1 , k< ...
- python exception的传递
try: block except1: except2: ... 如果block中出现了except,那么会先取匹配except1,如果匹配了,进行处理,程序继续执行. 如果except1没有匹配上, ...
- ThinkPHP使用方法与心得
ThinkPHP相信PHP程序员对它并不陌生,通过自己的学习在此发表个人看法,也为以后自己查找ThinkPHP方面的知识更加方便. 一.mvc及数据库CURD操作流程: 1.新建数据库:数据库名称:1 ...
- Codeforces Round #364 (Div. 1)(vp) 没什么题解就留坑待填
我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...
- golang 关于golang.org/x包问题
关于golang.org/x包问题 由于谷歌被墙,跟谷歌相关的模块无法通过go get来下载,解决方法: git clone https://github.com/golang/net.git $GO ...
- 【转载】如何在 C#中访问 JavaScript函数?
如何在 C#中访问 JavaScript函数? 时间:13-10-17 栏目:Unity3D教程 作者:zqcyou 评论:0 如何在 C#中访问 JavaScript函数?答案如下:c#代码 ...