题目要求

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.

题目分析及思路

要求判断一个给定词对字母大写的使用是否正确。正确使用需满足三个条件中的任意一个:1)全部大写;2)全部小写;3)当不只一个字母时,首字母大写,其余字母小写。可以先得到给定词中大写字母的个数,若和给定词长度相等,或者为0,又或者为1且该词首字母大写,则返回true,否则返回false。

python代码

class Solution:

def detectCapitalUse(self, word: str) -> bool:

count = 0

for c in word:

if c.isupper():

count += 1

if count == len(word) or count == 0 or (count==1 and word[0].isupper()):

return True

else:

return False

LeetCode 520 Detect Capital 解题报告的更多相关文章

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

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

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

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

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

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

  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 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. 单片机成长之路(51基础篇) - 004 STC89C52MCU 软件实现系统复位

    用户应用程序在运行过程中,有时会有特殊需求,需要实现单片机系统复位(热启动之一),传统的8051单片机由于硬件上未支持此功能,用户必须用软件模拟实现,实现起来较麻烦.STC单片机增加了相应的硬件功能, ...

  2. Android Studio 好用的设置

    Android Studio 好用的设置 设置目录 Getter 模板修改--自动处理 null 判断 格式化代码自动整理方法位置--广度 or 深度 设置步骤: Getter 模板修改,自动处理 n ...

  3. 如何保持github的fork于主干同步

    step1: https://help.github.com/articles/configuring-a-remote-for-a-fork/ step2: https://help.github. ...

  4. HADOOP security

    https://www.microsoft.com/en-us/trustcenter/security/azure-security https://docs.microsoft.com/en-us ...

  5. Thymeleaf模版--子页面单独引入CSS、JS文件

    https://blog.csdn.net/u010392801/article/details/80465800 ****************************************** ...

  6. C语言 · 数的划分

    算法训练 数的划分   时间限制:1.0s   内存限制:256.0MB        锦囊1 使用动态规划. 锦囊2 用F[i,j,k]表示将i划分成j份,最后一份为k的方案数,则F[i,j,k]= ...

  7. 【数据库】——SQLite使用drop column删除表字段

    由于项目需求变更,我需要在sqlite的表中删除一个字段,通用的sql操作语句如下: alter table task drop column custom_fields; 结果数据库提示如下错误: ...

  8. hdoj:2085

    核反应堆 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 三层构架和 MVC的区别和理解

    1.三层构架和 MVC 意思一样么? Java WEB 开发中,服务端通常分为表示层.业务层.持久层,这就是所谓的三层架构: 1.表示层负责接收用户请求.转发请求.生成数据的视图等: 2.业务层负责组 ...

  10. CentOS7安装Java还是无法使用javac

    centos7.4 安装java之后,还是无法使用javac命令.报错提示: [root@ip---- centos]# javac bash: javac: command not found 解决 ...