题目要求

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. ROC曲线-阈值评价标准

    ROC曲线指受试者工作特征曲线 / 接收器操作特性曲线(receiver operating characteristic curve), 是反映敏感性和特异性连续变量的综合指标,是用构图法揭示敏感性 ...

  2. Android 6.0 动态权限申请注意事项

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 And ...

  3. maven 使用 国内镜像的方法 解决依赖下载慢

    转自:http://blog.csdn.net/banqgg/article/details/55804569 Maven是当前流行的项目管理工具,但官方的库在国外经常连不上,连上也下载速度很慢.国内 ...

  4. python 字符编码判断 chardet评测

    之前一直想找到一个模块,针对字符判断是什么字符集编码的库 网上有chardet的blog,发现自己的环境有这个库,于是就做了测试 >>> import chardet >> ...

  5. SAP Tax Service可以取代TAXBRA / RVABRA吗?(翻译) 跨国贸易云税务解决方案

    这篇文章的内容基于Fausto Motter  的一篇文章,他在SAP社区用葡萄牙语写了一篇文章,讨论如何用云解决方案取代巴西税收计算.我翻译了他的部分文章,添加并修改了一些内容,目标是帮助全球的SA ...

  6. box-shadow比较美观的阴影

    无奈每次调阴影都特别丑,这次我把它记下来,下次不调了 box-shadow: 0 5px 15px -5px rgba(0,0,0,.5);

  7. redis主从同步配置

    主服务器无需操作,从服务器 SLAVEOF 主服务器IP 端口 即可. 断开主从,执行 SLAVEOF NO ONE 即可. 同事和我在内网配置了连内网测试的服务器可以执行,但是连对方的不行,开始以为 ...

  8. Docker-服务(4)

    服务定义 在分布式应用程序中,应用程序的不同部分称为“服务”.例如,如果您想象一个视频共享站点,它可能包括一个用于在数据库中存储应用程序数据的服务,一个用户在上传内容后在后台进行视频转码的服务,一个用 ...

  9. Rafy环境配置

    如果现在项目已经创建好啦,要使用Rafy框架进行存储,这里我简单的总结下配置的环境步骤: 一.添加引用Rafy的SDK,如下几个; 二.新建文件夹Entities 在此文件夹下创建rafy实体以及仓库 ...

  10. 《转载》JVM垃圾回收机制

    本文转载自ImportNew - 郑雯 每个Java程序员迟早都会碰到下面这个错误: java.lang.OutOfMemoryError 这个时候一般会建议采用如下方式解决这个错误: 增加MaxPe ...