leetCode Detect Capital
1、问题描述
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". All letters in this word are not capitals, like "leetcode".
2.Only the first letter in this word is capital if it has more than one letter, like "Google".
3.Otherwise, we define that this word doesn't use capitals in a right way.
检测一个string中的大写字母的对错,以下三种情况中,大写字母的格式是正确的。
1. 整个string全部是大写字母。
2.整个string全是小写字母。
3.单词中首字母大写。
2、问题分析
首先处理特殊情况,输入的string 长度是0 或者 1的时候,返回 true。
然后根据 前两个字母的大小分析。
bool detectCapitalUse(string word)
{
if( word.size() == || word.size() == )
return true; if( islower( word[] ) )
{
for(int i = ; i < word.size(); i++)
{
if( isupper(word[i]) )
return false;
}
} if( isupper( word[] ) && isupper(word[]) )
{
for( int i = ; i < word.size(); i++)
if( islower( word[i]) )
return false;
} if( isupper(word[]) && islower(word[]) )
{
for(int i = ; i< word.size(); i++)
if(isupper(word[i]))
return false;
} return true;
}
1. 如果第一个字母是小写,那么后续字母中出现 大写字母,就返回false.
2.如果前两个字母都是大写,那么后续的字母再出现小写字母就返回false。
3.如果第一个字母是大写,第二个字母是小写,那么后续再出现大写字母,就返回false。
3、代码
leetCode Detect Capital的更多相关文章
- LeetCode——Detect Capital
LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...
- [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 ...
- 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 ...
随机推荐
- 【JQuery源码】事件绑定
事件绑定的方式有很多种.使用了jQuery那么原来那种绑定方式(elem.click = function(){...})就不推荐了,原因? 最主要的一个原因是elem.click = fn这种方式只 ...
- 《Mysql技术内幕,Innodb存储引擎》——Innodb体系结构
Innodb体系结构 Innodb存储引擎主要包括内存池以及后台线程. 内存池:多个内存块组成一个内存池,主要维护进程/线程的内部数据.缓存磁盘数据,修改文件前先修改内存.redo log 后台线程: ...
- java学习-java.lang一Number类
java.lang包是java语言中被用于编程的基本包.编写的程序基本上都要用到这个包内的常用类. 包含了基本数据类型包装类(Boolean,Number,Character,Void)以及常用类型类 ...
- paxos协议更新日志
基于Paxos协议的数据同步与传统主备方式最大的区别在与Paxos只需任意超过半数的副本在线且相互通信正常,就可以保证服务的持续可用,且数据不丢失. Basic paxos协议更新日志 我们将数据持久 ...
- linux下软、硬链接的创建和删除
linux下软.硬链接的创建和删除 在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件时,索引结点被复 ...
- SQL、Linq和Lambda表达式 的关系
首先说说这三者完全是三种不同的东西,SQL是结构化查询语言(Structured Query Language)简称,这大家再熟悉不过了,下面主要介绍LINQ和Lambda表达式的基本概念以及同一查询 ...
- ie,你还能再浪一点不
一个div,设置了高度,并且溢出滚动 各位观众,当点击滚动条的时候,event.target应该是什么呢? 火狐,chrome都认为是点击了div,这个也很好理解,他是div的滚动条,自然应该算div ...
- win10 uwp 重启软件
在16299支持在软件自己重启,不需要让用户点击关闭然后启动,虽然我还不知道这个有什么用.本文告诉大家如何让软件关闭重新打开. 首先需要使用的版本是 16299 ,然后使用 RequestRestar ...
- swagger 集成asp.net Core2.1
首先通过nuget 安装 Swashbuckle.AspNetCore 1.在startup.cs 的configureService services.AddAutoMapper(); serv ...
- java绘图drawString位置的确定
根据api,很容易知道使用方式如下: 指定字符串和坐标即可. 但是简单认为字符串的起始位置就是左上顶点就错了,这样画起来每次的位置都不对,字体的大小不同,位置偏差很大.仔细看api注释后发现,y坐标是 ...