题目要求

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. Ubuntu apt-get彻底卸载软件包【转】

    原文地址:https://blog.csdn.net/get_set/article/details/51276609 最近对ubuntu卸载参数的详细程度了解不够:转载已了解查用. 如果你关注搜索到 ...

  2. linux内存管理之malloc、vmalloc、kmalloc的区别

    kmalloc kzalloc vmalloc malloc 和get_free_page()的区别 一.简述 1. kmalloc申请的是较小的连续的物理内存,虚拟地址上也是连续的.kmalloc和 ...

  3. system generator学习笔记【01】

    作者:桂. 时间:2018-05-18  18:26:50 链接:http://www.cnblogs.com/xingshansi/p/9045914.html 前言 学习使用system gene ...

  4. Java 设计模式专栏

    Java 设计模式之工厂模式学习心得 转:Java 设计模式之单例模式 转:  Java设计模式之建造者模式 转:Java设计模式之代理模式

  5. idea 使用正则表达式 进行匹配替换

    关于正则表达式 可以参考相应的笔记 另外 如果要提取正则表达式中匹配到的内容,使用$1 - $... 按顺序取(第一个表达式 到 第N个表达式匹配到的数据),  这点和linux正则获取的方式是一样的

  6. CobaltStrike3.12/13 破解

    更新3.13破解版 链接: https://pan.baidu.com/s/14e0tpVPzUhiAhYU2_jvBag 提取码: d9uf MacOS客户端: 链接: https://pan.ba ...

  7. hdoj:2076

    夹角有多大(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. HTTP 05 安全

    TCP 打包时加密, 报文传输时 加密 通过证书来判断是否是可以访问的客户端/服务器. 公开秘钥 进行加密, 随便被人知道 私有秘钥 进行解密, 自己保留 http + 加密 + 认证 + 完整性保护 ...

  9. linux添加新磁盘和创建分区

    Linux磁盘概念及其管理工具fdisk:http://www.linuxidc.com/Linux/2016-08/134664.htm 一. 进入linux虚拟机 右键 open in termi ...

  10. java.exe和javaw.exe的区别

    相同点:二者都是Java的虚拟机,用来执行Java程序 区别: 1. javaw.exe运行程序时不会输出控制台信息 (注:“w”就是window的意思). 使用案例  start.bat(y以下代码 ...