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的更多相关文章

  1. LeetCode——Detect Capital

    LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...

  2. [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 ...

  3. 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 ...

  4. Leetcode 520. Detect Capital 发现大写词 (字符串)

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

  5. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  6. 520. Detect Capital【easy】

    520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...

  7. 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 ...

  8. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  9. [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 ...

随机推荐

  1. [webrtc] rtcp模块中rtt时间计算

    RTT指 round-trip time,即计算AB两端的往返时延 这里可以分成两个问题: 如何在A端估算A和B之间的RTT时间? 如何在B端估算A和B之间的RTT时间? 本文参考资料:rfc 355 ...

  2. predefClass中包含的符号

    Scope[ ||(boolean,boolean), &&(boolean,boolean), !=(int,int), !=(long,long), !=(float,float) ...

  3. 04-python的列表操作

    python中列表的使用最多, 常用的方法有: append(value) extend(L) 添加指定列表的所有元素 insert(index, value) 插入 remove(value) po ...

  4. springboot-18-springboot的参数封装

    springboot的参数封装, 和springmvc相识 简单参数的封装 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * ...

  5. elasticsearch基本操作之--使用java操作elasticsearch

    /** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ es 查询共有4种查询类型 QUERY_AND_FETCH: ...

  6. 编程开发(C/C++&Java&Python&JavaScript&Go&PHP&Ruby&Perl&R&Erlang)

    使用Docker快速部署主流编程语言的开发.编译环境及其常用框架,包括C.C++.Java.Python.JavaScript.Go.PHP.Ruby.Perl.R.Erlang等. 在今后采用编程语 ...

  7. C语言中的按位移动及其简单引用

    C语言中的按位移动及其简单应用 在C语言中按位左移用”<<”表示,按位右移用”>>”表示. 按位左移和按位右移运算经常被用来替换乘二和除二运算,但是要注意,这两者之间并不完全等 ...

  8. DC综合及仿真验证和DFT测试

           综合技术是提高设计产能的一个很重要的技术,没有综合技术的发展就不可能用HDL实现电路的设计,因为HDL开始是用来供电路仿真建模使用的,到了后来才把其中一部分子集作为可综合的语言,也就是R ...

  9. json对象按时间排序

    //正序var data = {"rows": [{"name": "张三","time": "2011/4/ ...

  10. 二维码ZBar之ZBarReaderView

    参考:http://www.chinatarena.com/Html/iospeixun/201301/3985.html   http://blog.csdn.net/chenyong05314/a ...