1.问题描诉

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:
1. All letters in this word are capitals, like “USA”.
2. All letters in this word are not capitals, like “leetcode”.
3. 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.

来自 https://leetcode.com/problems/detect-capital/description/

2.题目分析

给定一个单词,判断其用法是否正确,正确的用法有以下三种:1.全部是大写;2.全部是小写;3.只有首字母是大写。因此,遍历一遍元素,计算出大写字母的个数cnt,如果cnt和单词的长度一样,说明满足条件1;如果cnt==0,则说明满足条件2;如果cnt==1且首字母大写,则满足条件3。

3.C++代码

//我的代码:(beats 57%)
bool detectCapitalUse(string word)
{int cnt = 0;for (int i = 0; i < word.length(); i++){if (word[i]>='A'&&word[i]<='Z')cnt++;}if (cnt == word.length() || cnt == 0)return true;else if (cnt == 1 && word[0] >= 'A'&&word[0] <= 'Z')return true;elsereturn false;
}

520. Detect Capital相关推荐

  1. LeetCode之路:520. Detect Capital

    一.引言 这道题有关于处理字符的大小写问题,对于熟悉字符的大小写处理函数非常有帮助. 这里粘出题目信息: Given a word, you need to judge whether the usa ...

  2. Detect Capital

    题目:Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or no ...

  3. LeetCode之Detect Capital

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

  4. C#LeetCode刷题之#520-检测大写字母(Detect Capital)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3947 访问. 给定一个单词,你需要判断单词的大写使用是否正确. ...

  5. LeetCode算法题-Detect Capital(Java实现)

    这是悦乐书的第251次更新,第264篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第118题(顺位题号是520).给定一个单词,你需要判断其中大写字母的使用是否正确.当下 ...

  6. LeetCode Detect Capital

    原题链接在这里:https://leetcode.com/problems/detect-capital/#/description 题目: Given a word, you need to jud ...

  7. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  8. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  9. 数据结构与算法 学习笔记(5):字符串

    数据结构与算法 学习笔记(5)- 字符串 本次笔记记录了LeetCode中关于字符串的一些问题,并给出了相应的思路说明和代码.题目编号与LeetCode对应,方便查找. 题目1:LeetCode 13 ...

最新文章

  1. Shady 深度学习课程
  2. C# TreeView 连续点击 不触发AfterCheck事件
  3. python3 web框架_循序渐进Python3(十二) --0--  web之框架
  4. Linux系统编程:pipe匿名管道的使用,实现linux命令下管道命令
  5. CodeVS 1014 装箱问题(DP)
  6. Linux 设备驱动的固件加载-转载
  7. 2016年计算机考研大纲,2016考研计算机大纲解析
  8. 八大顶层设计,决定企业融资难度
  9. Office excel2010如何用两个文件分别打开两个窗口
  10. 动态规划——(很特别的一个动态规划入门教程)#觉得原作者写的很有意思
  11. android emmc测试软件,鉴别EMMC/UFS如此简单!小工具一键识别闪存
  12. 国空三区三线思考之:Arcgis自上而下从左到右进行编号
  13. 程序员工资的7个段位,你的工资在哪个Level?
  14. Hololens开发学习笔记——TrackedHandJoint关节点详解
  15. 为了上班摸鱼我用Python制作了俄罗斯方块?
  16. ansys 定义变量(关联模型)
  17. SAS学习笔记(四)第8/9/10章
  18. IOS个人账户转公司账户,TPshop APP提交审核
  19. iOS灵动岛【电商秒杀】开发实践
  20. 音视频技术开发周刊 | 246

热门文章

  1. Linux无桌面环境下,实现USB的自动挂载/卸载
  2. 科大讯飞往届生招聘_我从飞往西雅图的最后一波设计采访中学到的东西
  3. 大盘点 | 2020年「21篇」医学影像算法最佳综述
  4. 7. 【可编程定时器8253】:外部引脚、内部结构特点、计数启动方式、6种工作方式、控制字格式、应用
  5. 树莓派linux led字符设备驱动(原子操作)
  6. EPICS -- asynRecord记录使用示例
  7. 00012__photoScissor__替换照片背景
  8. 投资百万运营网站 为何只坚持了一年?
  9. android 文件存储基础
  10. Python编程基础——编程环境准备工作