描述

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:

All letters in this word are capitals, like "USA".

All letters in this word are not capitals, like "leetcode".

Only the first letter in this word is capital if it has more than one letter, like >"Google".

Otherwise, we define that this word doesn't use capitals in a right way.

Example 1:

Input: "USA"

Output: True

Example 2:

Input: "FlaG"

Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

分析

每遇到一个大写字母,就将变量upCount加1,遍历结束后,如果要返回真,那么会满足下列条件之一:

  • upCount == word.size()。即所有字母均为大写
  • upCount == 0.即所有字母均为小写
  • upCount == 1.有且仅有第一个字母为大写。

否则,返回假。

代码如下:

class Solution {
public:
bool detectCapitalUse(string word) {
int upCount = 0; if(word.size() == 0)return false;
for(int i = 0; i != word.size(); ++i)
if(isupper(word[i]))++upCount;
if(upCount == word.size() || upCount == 0 || (upCount == 1 && isupper(word[0])))return true;
return false;
}
};

日常水题。。。

leetcode解题报告(30):Detect Capital的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  5. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  6. [LeetCode解题报告] 502. IPO

    题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...

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

  8. leetCode解题报告5道题(九)

    题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...

  9. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. response letter

    1.Firstly, we would like to thank you for your kind letter and for reviewers’ constructive commentsc ...

  2. 数据建模工具------EZMNL

    表结构设计器(EZDML) 表结构设计器EZDML1.5新版本发布,比以前介绍的1.2版本改进了很多,因此重新写了个介绍. 表结构设计,即所谓的数据建模,目前大家常用的同类著名工具有PowerDesi ...

  3. 组件系列之RabbitMQ

    官网: https://www.rabbitmq.com/getstarted.html 1.四种交换机 TODO... 2.消费者获取消息方式:推 和 拉 拉取:每次拉取一条,循环拉取需要的条数在批 ...

  4. 通过设置启用 Visual Studio 默认关闭的大量强大的功能提升开发效率

    原文:通过设置启用 Visual Studio 默认关闭的大量强大的功能提升开发效率 使用 Visual Studio 开发 C#/.NET 应用程序,以前有 ReSharper 来不足其各项功能短板 ...

  5. (八) Docker 部署 mongodb

    参考并感谢 官方文档 https://hub.docker.com/_/mongo 下载mongo镜像(不带tag标签则表示下载latest版本) docker pull mongo 启动 mongo ...

  6. CSS是什么

    css是层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. CSS不 ...

  7. 修改redhat7默认显示语言从中文为英文

    [delmore@localhost Desktop]$ su                               //切换到最高权限 Password:                    ...

  8. npm 安装卸载模块 & ionic插件安装与卸载

    npm安装模块 npm install xxx利用 npm 安装xxx模块到当前命令行所在目录 npm install -g xxx利用npm安装全局模块xxx 本地安装时将模块写入package.j ...

  9. ios开发之NSData

    NSData用于保存字节数组. 初始化 - (instancetype)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length free ...

  10. git stash 缓存本地修改 简介

    当我们在使用git的时候,又是会有这种情况:当新的需求了的时候.我们需要为此需求新建一个分支,再次分支上进行修改,当经过测试,提交代码时,在将其合并到主分支,或生产分支上. 但是有时候也有失误的时候, ...