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. java数据结构之(堆)栈

    (堆)栈概述栈是一种特殊的线性表,是操作受限的线性表栈的定义和特点•定义:限定仅在表尾进行插入或删除操作的线性表,表尾—栈顶,表头—栈底,不含元素的空表称空栈•特点:先进后出(FILO)或后进先出(L ...

  2. 关于Item

    0.Item The base class of all items, which implements default behavior. 项(Item)代表一个字节码中可以寻址的实体,有许多种类的 ...

  3. ssh 登录进入 docker container

    1.Container安装ssh服务,博主的linux是centos ① 安装ssh sudo yum install openssh-server #安装ssh服务器 service sshd st ...

  4. free函数使用时的注意事项。

    free函数是我们在写C语言程序时常用的函数,但是使用时需要注意,一不小心很肯能会引起吐核. 注意:free函数与malloc()函数配对使用,malloc函数释放申请的动态内存.对于free(p)这 ...

  5. 笔记二:python编码详解

    一:学习内容 python编码讲解 python编码说明 python中文乱码解决三部曲 二:python编码讲解 1. ASCII编码 美国信息交换标准代码(American Standard Co ...

  6. C# 新建 exe文件,并且自定义协议从浏览器中启动该程序

    1. C# 新建一个 exe 文件: 打开你的 vs ,[文件] ---> [新建] ---> [项目] 选择 Windows 窗体应用,并起一个名字: 接着该文件会在当前项目的 myap ...

  7. vue-webpack 做出来的项目部署到服务器上,点开是空白页(我这里把项目发布到git上)

    总结1: 从网上下的很多demo,用npm run dev 就可以启动项目,比如:vue-cli,为什么?因为vue-cli自动帮我们安装了express服务器. 总结2: npm run dev 是 ...

  8. 安装ftp服务

    1.首先判断你服务器上是否安装了vsftpd 2.安装vsftpd 3.配置文件/etc/vsftpd/vsftpd.conf 禁止匿名用户登录,把YES改为NO,默认为YES 限制ftp用户跳出家目 ...

  9. Red Hat Linux 无法使用yum命令

    一:首先提供部分Red Hat 镜像下载地址 1.rhel-server-6.8-i386-dvd.iso 链接: https://pan.baidu.com/s/18VqxRgBMuAJE7Ty0H ...

  10. C#的async和awaiit的一些记录

    一,最近在学习abp的东西.发现其中作者在获取数据时,大量的使用了async和awaiit.好吧,因为不太懂,所以网上搜搜,在这里记录一下. 先来看一段代码.调用UpdateUserAsync会返回一 ...