LeetCode 520 Detect Capital 解题报告
2024-08-22 16:20:25
题目要求
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:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- 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
最新文章
- 构建Web API服务
- script标签中defer和async属性的区别
- 导入android-support-v4.jar的方法
- Verilog学习笔记基本语法篇(十二)········ 编译预处理
- Android--Activity(活动)
- CF721C. Journey[DP DAG]
- IDEA之google style配置(IDEA)
- 使用Eclipse构建Maven项目 (转)
- tachyon with spark
- Spring MVC Junit4 单元測试 JunitTest
- mysql主从同步报错
- jquery mobile 入门
- UVa 412 - Pi
- 2.postman安装及使用
- Jenkins插件之显示构建时间
- Vagrant 入门指南
- php 会话控制(禁用cookie后session为什么会失效?)
- Java CMYK图片转RGB图片(TwelveMonkeys方式)
- 总结: Sort 排序算法
- Asp.net Mvc5的认识