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] RTX的处理

    以前笔记,整理 webrtc中默认开启rtx用于丢包重传,rtx的介绍可以参考rfc4588,https://tools.ietf.org/html/rfc4588#section-4 rtx使用额外 ...

  2. HDFS DATANODE 磁盘容量的最小值

    HDFS的DATANODE的剩余空间具体要到多大?关于这个问题,下面记录下对这个问题的调查 昨天,讨论群里面给出了一个异常: op@odbtest bin]$ hadoop fs -put ../tm ...

  3. SpringMVC+AJAX+JSON

    在做一个ajax发送json到springmvc的控制层,控制层的对象中有一个List集合,ajax调用总是报415错误.发现了一个一直没有注意到的问题,借机记录一下. (细节部分都忽略了,在最后的d ...

  4. postman—环境切换和设置变量

    postman提供了environment管理功能,想要在多个环境中测试,比如在测试环境.灰度环境.生产环境等,只需要用同样的接口,切换下环境即可,非常方便.具体步骤: 一.切换环境 1.点击界面右上 ...

  5. Oracle 相关查询

    --创建用户 create user zzg identified by zzg123; --修改用户的密码 alter user zzg identified by unis; --所有用户所在的表 ...

  6. 回退Ubuntu记录

    前言 由于Ubuntu18经常出错,因而决定回退Ubuntu16,下面是记录回退问题及美化,以便以后需要. 问题总结 磁盘挂载 挂载其他磁盘分区时,提示错误"Metadata kept in ...

  7. java学习-get和post请求

    摘要 看完本文可以知道,使用java原生编写get/post请求的步骤,进行网络请求时应该注意的地方. 这里使用java自带的HttpUrlConnection对象进行网络请求, 请求步骤大致分为五步 ...

  8. 深度学习--RNN,LSTM

    一.RNN 1.定义 递归神经网络(RNN)是两种人工神经网络的总称.一种是时间递归神经网络(recurrent neural network),另一种是结构递归神经网络(recursive neur ...

  9. test11

    -Xms512m-Xmx512m-XX:PermSize=512-XX:MaxPermSize=512

  10. docker 创建elasticsearch集群镜像

    搞了2天终于搞好了 更新: 2017/2/15: 更改elasticsearch.yml中绑定ip, 可开启集群效果 //: supervisord.conf 加入 autostart=true, 开 ...