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. 链表问题16——单链表的选择排序
  2. ElementUI的el-form怎样格式化布局
  3. 读书笔记 effective c++ Item 16 成对使用new和delete时要用相同的形式
  4. 想追身边的女孩?哥们我只能帮到你这份上了
  5. asp.net MD5加密函数(c#)
  6. iOS:图片相关(19-05-09更)
  7. MapInfo MapXtreme 2005 WebGIS 简单鹰眼设计(转)
  8. Redis命令小细节
  9. vue设置多选框默认勾选_vue中复选框怎么默认全选,至少选择4个才可以点击下一步...
  10. launch images source启动图删除后上下有黑边
  11. 视频压缩工具linux,Ubuntu视频转换工具-mencoder命令行参数
  12. 下单送奖励金的实现思路
  13. 按钮控制android progressbar,Android ProgressBar手动控制开始和停止
  14. windows下ch340 usb转串口芯片的驱动从哪里下载?
  15. P1012 拼数(水题)
  16. 2021-09-02AUC
  17. 我们系有个计算机中心英语,吉大自考计算机专业英语(一)
  18. 串口、Modbus通信协议
  19. 大学计算机课程日记,大学计算机实习日记
  20. 用python制作动态二维码

热门文章

  1. Vlan(虚拟局域网配置)
  2. 手机NFC如何格式化加密IC卡
  3. grep 忽略大小写、忽略grep命令本身
  4. 项目设计-基于SpringBoot和Vue开发的宿舍管理系统
  5. bzoj1755 [Usaco2005 qua]Bank Interest
  6. opencv android安装教程,opencv for android安装教程.doc
  7. html弹窗广告文件怎么做,视频加入广告 如何在视频文件中加入弹窗广告
  8. mysql班级学生 一对多怎样设计表_MySQL的多表设计
  9. 浏览器有新消息之后,图标在电脑任务栏闪烁提示
  10. macOS硬盘如何格式转换?用Tuxera NTFS就够了!