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

题目描述

已知一个单词,你需要给出它是否为"大写词"

我们定义的"大写词"有下面三种情况:

  1. 所有字母都是大写,比如"USA"
  2. 所有字母都不是大写,比如"leetcode"
  3. 只有第一个字母是大写,比如"Google"

测试样例

Input: "USA"
Output: True Input: "FlaG"
Output: False

详细分析

水题,按照上面定义的三种情况写代码即可。

稍微值得注意的是如果字符串为空输出是true

代码实现

class Solution {
public:
bool detectCapitalUse(string word) {
if (word.length() == 0) {
return true;
}
bool capitalFirst = false;
int capitalCnt = 0;
if (isupper(word[0])) {
capitalFirst = true;
capitalCnt++;
}
for (int i = 1; i < word.length(); i++) {
if (isupper(word.at(i))) {
capitalCnt++;
}
}
if (capitalCnt == 0 ||
capitalCnt == word.length() ||
(capitalFirst && capitalCnt == 1)) {
return true;
}
return false;
}
};

Leetcode 520. Detect Capital 发现大写词 (字符串)的更多相关文章

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

  2. LeetCode 520 Detect Capital 解题报告

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

  3. LeetCode: 520 Detect Capital(easy)

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

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

  5. 520 Detect Capital 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的:    全部字母都是大写,比如"USA".    单词中所有字母都不是大写,比如&q ...

  6. 【leetcode】520. Detect Capital

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

  7. 520. Detect Capital【easy】

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

  8. 【LeetCode】520. Detect Capital 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...

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

随机推荐

  1. vue实用难点讲解

    此篇文章是我基于研究vue文档三遍的基础上,觉得还有点难理解或者难记的知识点总结 列表渲染 1.渲染组件必须加key,并且属性是手动传递给组件的 <my-component v-for=&quo ...

  2. CAD库中列举所有航路点

    select distinct f1.airway_point_name,f1.latitude,f1.longitude,upper(f1.airway_point_type_name)type,f ...

  3. 注解:@interface 自定义注解的语法

      自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口 ...

  4. oracle 启动停止过程

    oracle 主要由两部分组成:instance和database .instance是指一组后台进程/线程和一块共享内存区域,而database是指存储在磁盘上的一组物理文件. 数据库启动包括三个步 ...

  5. 安装zabbix-agent报错 Error: failure: repodata/primary.xml.gz from zabbix: [Errno 256] No more mirrors to try.

    安装zabbix-agent报错 yum install -y zabbix-agent Loaded plugins: fastestmirror, refresh-packagekit, secu ...

  6. ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?(图文详解)

    不多说,直接上干货! 问题详情 ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?  解决办法 是由于预装的vim软件没更新,运行   sudo apt-get install vi ...

  7. cocos2d-js动作模块使用(自用,只有代码)

    // var UIBase = require("src/views/ui/UIBase.js")// cc.loader.loadJs("src/views/ui/UI ...

  8. ruby 访问权限

    ##################### # 访问权限 ##################### class HeidSoft ##默认方法 def method1 ##### end prote ...

  9. MarkdownPad 2 安装和破解

    MarkdownPad 2 安装和破解 下载:http://markdownpad.com/ 下载下面这个: 破解:http://w3cboy.com/post/2014/10/MarkdownPad ...

  10. Excel课程学习

    1.Excel软件简介 1.1历史上的其他数据处理软件与Microsoft Excel 1977年,苹果公司开发了一款数据处理软件,当时这款软件卖的非常好,用软件的尾巴摇动硬件的狗,当时有人因为这款软 ...