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 ...
随机推荐
- 如何用 纯C++(ndk)开发安卓应用 ?
视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 如何安装安卓的开发环境以及怎么设置ndk的环境变量等在前边的文 ...
- C语言初学
输出控制符 输出控制符 含义 %d int 整型数 %ld long int %c char 一个字符 %f float 浮点数,整数/整数=整数,整数/小数=小数 %lf double %x %X ...
- java学习-struts基础(一)
struts发展 struts是Apache软件基金会赞助的一个开源项目,是一个基于Java EE的MVC开源实现. 它为Servlet/JSP技术的应用提供技术框架2001.7--Struts1正式 ...
- [LeetCode] Level Order Traversal
题目说明 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to r ...
- redis实战笔记(9)-第9章 降低内存占用
本章主要内容 1.短结构( short structure) 2.分片结构( shared structure) 3.打包存储二进制位和字节 本章将介绍3种非常有价值的降低Redis内存占用的 ...
- 原生JavaScript 导出excel表格(兼容ie和其他主流浏览器)
因同事的需求是想前端导出excel表格,网上找了一些demo,自己修改了一下,可能以后会用到,记录下来吧,兼容ie和一些主流浏览器,ie可能会报错,原因参考 这里,edge 浏览器还没有办法导出,正在 ...
- elasticsearch启动错误解决
es启动默认不能使用root用户,所以需要新创建一个用户来启动. 启动时可能出现的问题: [1]: max file descriptors [4096] for elasticsearch proc ...
- oracle 一对多数据分页查询筛选
今天项目测试运行的时候,遇到了一个奇怪的问题,这个问题说起来按sql语法的话是没有错误的 但是呢按照我们的业务来做区分就有些逻辑上的错误了, 下面请听我慢慢道来,在数据库中有两个数据, 先来看下第一次 ...
- 【转】JavaScript代码性能优化总结
本文作者:zifan 来自:携程设计委员会 链接:http://ued.ctrip.com/blog/javascript-code-performance-optimization-summary. ...
- Golang panic用法
Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...