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 ...
随机推荐
- EF 通过DataAnnotations配置属性和类型
一.通过Attribute配置约束 1.主键约束 通过KeyAttribute来配置主键约束,代码如下: [Key] public int PrimaryKey{ get; set; } 2.外键约束 ...
- IDEA Community(社区版)再谈之无奈之下还是去安装旗舰版
不多说,直接上干货! 前言 相信很多人,跟我一样,一开始,接触spark,肯定会首选IntelliJ IDEA的社区版Community. IntelliJ IDEA号称当前Java开发效率最高的ID ...
- glide 解决 golang.org/x/net 等依赖包无法获取
知道glide有设置镜像功能,可以把某个依赖包的源地址切换为另一个地址,相当于切换到镜像地址,用于某些依赖包被墙的原因 之前碰到 golang.org/x/net,设置镜像: glide mirror ...
- Nginx 为 Golang 配置 web 服务
server { charset utf-; client_max_body_size 128M; #listen ; ## 监听 ipv4 上的 端口 #listen [::]: default_s ...
- centos7-安装mysql5.6.36
本地安装了mysql5.7, 但和springboot整合jpa时会出现 hibernateException, 不知道为什么, 换个mysql5.6版本的mysql, 源码安装, cmake一直过 ...
- C++中内联函数
目录 什么是内联函数 如何使函数内联 为什么要使用内联函数 inline函数的优缺点分析 什么时候该使用内联函数 正文 在C语言中,我们使用宏定义函数这种借助编译器的优化技术来减少程序的执行时间,那么 ...
- 任务三十七:UI组件之浮出层
任务三十七:UI组件之浮出层 面向人群: 有一定JavaScript基础 难度: 低 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量 ...
- javascript正则表达式获取控制
正则表达式的元字符是包含特殊含义的字符,他们有一些特殊的功能,可以控制匹配模式的方式,反斜杠后的元字符将失去其特殊含义 单个字符 元字符 匹配情况 . 匹配除换行符外的任意字符 [a-z0-9] 匹配 ...
- gdb中run出现的Missing separate debuginfos, use: debuginfo-install XXX
问题: Missing separate debuginfos, use: debuginfo-install glib 解决方法: 1.将/etc/yum.repo.d/CentOS-Debugin ...
- [日常] nginx反代websocket
去年的事 , 随便记记 ============================================================= 2017年11月6日 记录: 获取包的选择状态: d ...