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 ...
随机推荐
- JavaScript读书笔记(2)--数据类型
1. 严格模式:在javascript中定义了一种不同的解析与执行模型.在严格模式下,一些不确定的行为将得到处理,对某些不安全的操作也会抛出错误. 用法是在脚本中添加:”use strict”; 这 ...
- C++输入一行字符串的一点小结
C++输入一行字符串的一点小结 原文链接: http://www.wutianqi.com/?p=1181 大家在学习C++编程时.一般在输入方面都是使用的cin. 而cin是使用空白(空格,制表符和 ...
- StringUtils类使用 (转载)
检查字符串是否为空或null或仅仅包含空格 String test = ""; String test1=" "; String test2 = &quo ...
- 九度OJ 1101:计算表达式 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4340 解决:1335 题目描述: 对于一个不存在括号的表达式进行计算 输入: 存在多种数据,每组数据一行,表达式不存在空格 输出: 输出结 ...
- Understanding Unicorn and unicorn-worker-killer Unicorn
We just wrote some new documentation on how Gitlab uses Unicorn and unicorn-worker-killer, available ...
- Delphi快捷键大全
Delphi快捷键大全 在过程.函数.事件内部, SHIFT+CTRL+向上的方向键 可跳跃到相应的过程.函数.事件的定义.相反,在过程.函数.事件的定义处,SHIFT+CTRL+向下的方向键 可跳跃 ...
- 初学php html javascript后小总结
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/c3568/article/details/30474015 转载请注明出处:http://blog. ...
- maven资料
1.配置settings.xml:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extensio ...
- Vue实例和方法
github地址:https://github.com/manlili/vue_learn里面的lesson03 一 实例 每个 Vue 实例都会代理其 data 对象里所有的属性,改变data,vu ...
- Yii 表单验证规则---总结
Filter: 过滤,'filter'=>'trim',表示去空格 Required:必须的,表示不能为空 Match: 匹配正则,需要和pattern一起使用,定义正则表达式,'pattern ...