描述

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. 小程序的数据监听 用法和vue中的watch一样====使用通配符监听所有自数据字段的变化

    使用通配符监听所有自数据字段的变化

  2. yield再理解--绝对够透彻

    首先,拿好宝剑: 先把yield看做“return”, 普通的return是什么意思,就是在程序中返回某个值,返回之后程序就不再往下运行了. 看做return之后再把它看做一个是生成器(generat ...

  3. ApachShiro 一个系统 两套验证方法-(后台管理员登录、前台App用户登录)同一接口实现、源码分析

    需求: 在公司新的系统里面博主我使用的是ApachShiro 作为安全框架.作为后端的鉴权以及登录.分配权限等操作 管理员的信息都是存储在管理员表 前台App 用户也需要校验用户名和密码进行登录.但是 ...

  4. 机器学习 降维算法: isomap & MDS

    最近在看论文的时候看到论文中使用isomap算法把3D的人脸project到一个2D的image上.提到降维,我的第一反应就是PCA,然而PCA是典型的线性降维,无法较好的对非线性结构降维.ISOMA ...

  5. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  6. Java CPU占用过高问题排查,windows和Linux

    LINUX系统: linux系统比较简单: 1.使用命令 ps -ef | grep 找出异常java进程的pid.  找出pid为 20189 2. top -H -p 20189,所有该进程的线程 ...

  7. XML 约束

    XML约束 一.约束 约束:规定 xml 文档的书写规则 要求: 1.能够在 xml 中引入约束文档 2.能够简单的读懂约束文档 分类: 1.DTD:一种简单的约束技术(后缀.dtd) 2.Schem ...

  8. day31-python之内置函数

    1.udp多线程 import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): prin ...

  9. zubax_gnss简介

    参考:https://docs.zubax.com/zubax_gnss 目录结构 bootloader:主要功能是升级application.加载application和初始化application ...

  10. [daily][archlinux] pacman 安装软件时404的问题

    时常,我们在archlinux上pacman安装一个软件时,会遇见如图这样的问题: “The requested URL returned error: 404”  [classic_tong @ 2 ...