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 ...
随机推荐
- Canvas学习笔记——拖曳与投掷物体
首先用一个例子来演示这个效果: 鼠标可以拖曳和投掷小球 // > 16 & 0xff, g = color >> 8 & 0xff, b = color > ...
- 多线程(C++)临界区Critical Sections
一 .Critical Sections(功能与Mutex相同,保证某一时刻只有一个线程能够访问共享资源,但是不是内核对象,所以访问速度比Mutex快,但是没有等待超时的功能,所以有可能导致死锁,使用 ...
- python2&python3的区别
区别1. python3中>>>range<3,6>range<3,6> python2中>>>range<3,6>[3,4,5 ...
- java之异常的捕获及处理
在java中程序的错误主要是语法错误和语义错误(也就是逻辑错误). java中异常处理语句的格式: try{ //有可能出现异常的语句 }catch(异常类 异常对象){ //编写异常的处理语句 }c ...
- java 内部类(转)
原文: http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 ...
- android——实现多语言支持
我们知道,建好一个android 的项目后,默认的res下面 有layout.values.drawable等目录.这些都是程序默认的资源文件目录,如果要实现多语言版本的话,我们就要添加要实现语言的对 ...
- IC卡、ID卡、M1卡、射频卡的区别是什么
IC卡.ID卡.M1卡.射频卡都是我们常见的一种智能卡,但是很多的顾客还是不清楚IC卡.ID卡.M1卡.射频卡的区别是什么,下面我们一起来看看吧. 所谓的IC卡就是集成电路卡,是继磁卡之后出现的又一种 ...
- 狂配Nginx
一 .Nginx虚拟主机配置( 基于不同的域名,跳转到不同的项目) 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部 ...
- 《数学之美》第15章 矩阵计算和文本处理中两个分类问题——SVD分解的应用
转载请注明原地址:http://www.cnblogs.com/connorzx/p/4170047.html 提出原因 基于余弦定理对文本和词汇的处理需要迭代的次数太多(具体见14章笔记),为了找到 ...
- 驻守深寒:寻找那些有效地关键K线
K线是组成投机市场的基本符号,也是技术分析的基本工具.可是面对浩如烟海的杂乱K线,特别是市场盘整时,经常使人们的判断发生混乱.支撑之下有支撑,阻力之上有阻力. 前人总结了大量的K线组合和由K线组成的技 ...