作者: 负雪明烛
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:

  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.

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)的更多相关文章

  1. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  2. Leetcode 520. Detect Capital 发现大写词 (字符串)

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 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 ...

  5. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  6. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  8. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  9. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. Redis集合解决大数据筛选

    Redis集合:集合是什么,就是一堆确定的数据放在一起,数学上集合有交集.并集的概念,这个就可以用来做大数据的筛选功能. 以商品为例,假如商品有颜色和分类.价格区间等属性. 给所有统一颜色的商品放一个 ...

  2. Visual Studio Code常用操作整理

    Live Server插件可以在保存html文件后实时地刷新页面 在html文件中键入"! +Tap"会生成一个html模板 保存文件:Ctrl+S 文件跳转:Ctrl+P 文件内 ...

  3. 为 Rainbond Ingress Controller 设置负载均衡

    Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.rbd-gateway 组件,实际上是好雨科技团队开发的一种 Ingress Contro ...

  4. 15. Linux提取RPM包文件(cpio命令)详解

    在讲解如何从 RPM 包中提取文件之前,先来系统学习一下 cpio 命令.cpio 命令用于从归档包中存入和读取文件,换句话说,cpio 命令可以从归档包中提取文件(或目录),也可以将文件(或目录)复 ...

  5. Android权限级别(protectionLevel)

    通常情况下,对于需要付费的操作以及可能涉及到用户隐私的操作,我们都会格外敏感. 出于上述考虑以及更多的安全考虑,Android中对一些访问进行了限制,如网络访问(需付费)以及获取联系人(涉及隐私)等. ...

  6. 3.6 String 与 切片&str的区别

    The rust String  is a growable, mutable, owned, UTF-8 encoded string type. &str ,切片,是按UTF-8编码对St ...

  7. Java Web 实现Mysql 数据库备份与还原

    前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...

  8. Linux学习 - 文件系统属性chattr权限

    change file attributes on 啊linux file system 1 功能 可以防止误操作 2 chattr命令格式 chattr [+-=] [选项] 文件或目录名 + 增加 ...

  9. Linux_spool命令

    spool的作用是什么? spool的作用可以用一句话来描述:在sqlplus中用来保存或打印查询结果. 参数指南 对于SPOOL数据的SQL,最好要自己定义格式,以方便程序直接导入,SQL语句如: ...

  10. Ribbon详解

    转自Ribbon详解 简介 ​ Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让 ...