【LeetCode】520. Detect Capital 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/detect-capital/#/description
题目描述
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.
Example :
Input: "USA"
Output: True
Input: "FlaG"
Output: False
题目大意
判断一个字符串是否满足大写规则:全部大写,开头大写后面小写,全部小写。
解题方法
循环判断三个条件
往往简单的方法就最有效。这个题说了有三种情况,那我就判断三种情况,如果有一个情况满足就可以。在判断是否满足全大写的时候,如果有一个字母是小写,那么就判断为false,然后break;就好了。就这样判断了三次,最后如果有一个是true,即可返回是。
这样的一个缺点就是如果已经判断是全大写,还要判断其他的,当然可以写if语句判断,但是我感觉有点啰嗦,还不如直接三个情况都判断简单。
public class Solution {
public boolean detectCapitalUse(String word) {
if(word == null || word.length() == 0){
return false;
}
int len = word.length();
boolean isUpper = true;//全大写
boolean isLower = true;//全小写
boolean isFirst = true;//首字母大写
for(int i = 0; i < len; i++){
if(word.charAt(i) >= 'a' && word.charAt(i) <= 'z'){
isUpper = false;
break;
}
}
for(int i =0; i < len; i++){
if(word.charAt(i) >= 'A' && word.charAt(i) <= 'Z'){
isLower = false;
break;
}
}
if(word.charAt(0) >= 'a' && word.charAt(0) <= 'z'){
isFirst = false;
}
for(int i = 1; i < len; i++){
if(word.charAt(i) >= 'A' && word.charAt(i) <= 'Z'){
isFirst = false;
break;
}
}
return isLower || isUpper || isFirst;
}
}
大写字母个数和位置判断
看到了别人的更简洁的做法,统计大写字母的个数,有0个或者全是大写,或者只有一个大写并且在首位。
Java版本:
public class Solution {
public boolean detectCapitalUse(String word) {
int cnt = 0;
for(char c: word.toCharArray()) if('Z' - c >= 0) cnt++;
return ((cnt==0 || cnt==word.length()) || (cnt==1 && 'Z' - word.charAt(0)>=0));
}
}
python版本:
class Solution(object):
def detectCapitalUse(self, word):
"""
:type word: str
:rtype: bool
"""
count = 0
for i, w in enumerate(word):
if w.isupper():
count += 1
return count == len(word) or count == 0 or (word[0].isupper() and count == 1)
根据首字符判断
如果首字符是大写,那么后面可以全部小写或者全部小写。
如果首字符是小写,那么后面只能全部小写。
python版本:
class Solution(object):
def detectCapitalUse(self, word):
"""
:type word: str
:rtype: bool
"""
if word[0].isupper():
return all(map(lambda w : w.isupper(), word[1:])) or all(map(lambda w : w.islower(), word[1:]))
else:
return all(map(lambda w : w.islower(), word[1:]))
日期
2017 年 4 月 3 日
2018 年 11 月 10 日 —— 这么快就到双十一了??
【LeetCode】520. Detect Capital 解题报告(Java & Python)的更多相关文章
- LeetCode 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 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 ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
随机推荐
- 毕业设计之LVS+keealive 负载均衡高可用实现
环境介绍 centos6.9最小化安装 主机名 IPADDR lvsA.load.quan.bbs 192.168.111.131 lvsB.load.quan.bbs 192.168.111.132 ...
- shell 小数的比较大小问题
经过实验,if 语句中的数值判断是不可以比较小数大小的:-gt -ne 2. 直接用awk awk -v num1=6.6 -v num2=5.5 'BEGIN{print(num1>num ...
- kubernetes部署kube-controller-manager服务
本文档介绍部署高可用 kube-controller-manager 集群的步骤. 该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态.当 leader ...
- Requests的安装和使用
一.Requests的安装1.pip3 install requests2.验证 import requests 不报错即可
- typora 图床配置方法
学习计算机的同学,在日常学习中难免会记笔记,写文档.相信大家记笔记大部分使用的都是 Markdown 吧,如果到现在还没接触,那我强烈建议你去学习一下,大概几分钟就可以搞定它. 注:下文用到的所有软件 ...
- Spring Cloud中五花八门的分布式组件我到底该怎么学
分布式架构的演进 在软件行业,一个应用服务随着功能越来越复杂,用户量越来越大,尤其是互联网行业流量爆发式的增长,导致我们需要不断的重构应用的结构来支撑庞大的用户量,最终从一个简单的系统主键演变成了一个 ...
- JSP内置对象之out对象
一. JSP内置对象的概述 由于JSP使用java作为脚本语言,所以JSP将具有强大的对象处理能力,并且可以动态地创建Web页面内容.但Java语法在使用一个对象前,需要先实例化这 ...
- MapReduce05 框架原理OutPutFormat数据输出
目录 4.OutputFormat数据输出 OutputFormat接口实现类 自定义OutputFormat 自定义OutputFormat步骤 自定义OutputFormat案例 需求 需求分析 ...
- 学习java 7.9
学习内容: Date类 Date类常用方法 SimpleDateFormat 1.格式化(从Date到String) public final String format(Date date) 将日期 ...
- ORACLE lag,lead
oracle中想取对应列前几行或者后几行的数据时可以使用lag和lead分析函数 lag:是滞后的意思,表示本行数据是要查询的数据后面,即查询之前行的记录. lead:是领队的意思,表示本行数据是要查 ...