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

题目描述

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

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

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

测试样例

  1. Input: "USA"
  2. Output: True
  3. Input: "FlaG"
  4. Output: False

详细分析

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

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

代码实现

  1. class Solution {
  2. public:
  3. bool detectCapitalUse(string word) {
  4. if (word.length() == 0) {
  5. return true;
  6. }
  7. bool capitalFirst = false;
  8. int capitalCnt = 0;
  9. if (isupper(word[0])) {
  10. capitalFirst = true;
  11. capitalCnt++;
  12. }
  13. for (int i = 1; i < word.length(); i++) {
  14. if (isupper(word.at(i))) {
  15. capitalCnt++;
  16. }
  17. }
  18. if (capitalCnt == 0 ||
  19. capitalCnt == word.length() ||
  20. (capitalFirst && capitalCnt == 1)) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. };

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. python学习笔记(一):python简介和入门

    最近重新开始学习python,之前也自学过一段时间python,对python还算有点了解,本次重新认识python,也算当写一个小小的教程.一.什么是python?python是一种面向对象.解释型 ...

  2. ghld data format

    %CTF: 1.00%FileType: PROF strp "VelocityProfile"%PROFSpec: 1.00 2006 00 00%Manufacturer: C ...

  3. 01-20Asp.net--Linq语法

    Linq语法--语言集成查询 同Sqlserver语句,但顺序颠倒了. 使用方法: 新建Linq类.dbml结尾的文件 在web窗体源代码中设计表,使用Repeater中转存放: <asp:Re ...

  4. django的render的说明

    return render(request,"homesite.html",locals()) homesite.html页面中的所有内容都可以被渲染,不论是标签还是js代码,包括 ...

  5. js-tree坑

    今天遇到一个js坑,一个页面,有两棵树,用同一个套参数初始化的,,,,当选择完另一个棵树之后,再操作另一颗树,不选择树节点,就会有错误出现,,,

  6. NLTK词性标注解释

    1.      CC      Coordinating conjunction 连接词2.     CD     Cardinal number  基数词3.     DT     Determin ...

  7. 204. Count Primes 统计<n的质数的个数

    [抄题]: Count the number of prime numbers less than a non-negative number, n. [暴力解法]: 时间分析: 空间分析: [优化后 ...

  8. 数字图像处理实验(5):PROJECT 04-01 [Multiple Uses],Two-Dimensional Fast Fourier Transform 标签: 图像处理MATLAB数字图像处理

    实验要求: Objective: To further understand the well-known algorithm Fast Fourier Transform (FFT) and ver ...

  9. ubuntu下各种解压缩文件方式

    tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunzi ...

  10. Python程序设计8——网络编程

    Python是一个很强大的网络编程工具,python内有很多针对场景网络协议的库,在库顶部可以获得抽象层,这样就可以集中精力在程序的逻辑处理上,而不是停留在网络实现的细节中. 1 少数几个网络设计模块 ...