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. select *和select 全部

    select *和select 全部字段 在查询上效果是一样的,速度也是一样的. 不过理论上来说select *反而会快点. 因为 1.select 全部字段在数据传输上消耗会更多,如果几百个字段这个 ...

  2. 2015.3.11 VS异步控件及进度条结合应用

    1.在Form中添加 指针控件:BackgroundWorker-bgwork:进度条控件progressBar1 以及开始.取消按钮 2.开始按钮启动异步线程 private void button ...

  3. Delphi Cookie

    Cookie IdHTTP1.CookieManager.AddCookies(); IdHTTP1.Post(); IdHTTP1.Get('http://1.1.1.1:9000/'); for ...

  4. findall查找 ^$*+?{ }{m,n}[].[.] \w \s \d \b \D \W

    #!/usr/bin/env python import re r = "aasa da.5a5dfgfda ada" ret = re.findall('a',r) print( ...

  5. android-auto-scroll-view-pager (无限广告轮播图)

    github 地址: https://github.com/Trinea/android-auto-scroll-view-pager Gradle: compile ('cn.trinea.andr ...

  6. java中用正则表达式判断中文字符串中是否含有英文或者数字

    public static boolean includingNUM(String str)throws  Exception{ Pattern p  = Pattern.compile(" ...

  7. 【总结整理】WebGIS学习-thinkGIS(二):关于level,比例尺scale,分辨率resolution

    1.Level包含了一个resolution参数和一个scale参数 瓦片本身: 我们用arcgis切完图后,打开发布的服务或者打开config.xml配置文件,可以看到所切之图的相关配置.如图所示: ...

  8. Struts2框架04 struts和spring整合

    目录 1 servlet 和 filter 的异同 2 内存中的字符编码 3 gbk和utf-8的特点 4 struts和spring的整合 5 struts和spring的整合步骤 6 spring ...

  9. 算法Sedgewick第四版-第1章基础-2.3 Quicksort-001快速排序

    一. 1.特点 (1)The quicksort algorithm’s desirable features are that it is in-place (uses only a small a ...

  10. JavaWeb_内省(Instrospector)

    内省是什么? 开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都是用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作Java对象的属性. 什么是Java对象的 ...