题目要求

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

最新文章

  1. 构建Web API服务
  2. script标签中defer和async属性的区别
  3. 导入android-support-v4.jar的方法
  4. Verilog学习笔记基本语法篇(十二)········ 编译预处理
  5. Android--Activity(活动)
  6. CF721C. Journey[DP DAG]
  7. IDEA之google style配置(IDEA)
  8. 使用Eclipse构建Maven项目 (转)
  9. tachyon with spark
  10. Spring MVC Junit4 单元測试 JunitTest
  11. mysql主从同步报错
  12. jquery mobile 入门
  13. UVa 412 - Pi
  14. 2.postman安装及使用
  15. Jenkins插件之显示构建时间
  16. Vagrant 入门指南
  17. php 会话控制(禁用cookie后session为什么会失效?)
  18. Java CMYK图片转RGB图片(TwelveMonkeys方式)
  19. 总结: Sort 排序算法
  20. Asp.net Mvc5的认识

热门文章

  1. go-ehtereum编译:
  2. 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
  3. blender show normals
  4. ffmpeg中AVOption的实现分析
  5. input文件上传(上传单个文件/多选文件/文件夹、拖拽上传、分片上传)
  6. C语言 · 单词数统计
  7. linux下启动和关闭tomcat服务的方式
  8. docker被屏蔽后下载方法
  9. 《FPGA全程进阶---实战演练》之搞定阻抗匹配
  10. 解决vscode无法安装golang相关插件的问题 - 即无法直连golang.org的问题