作者: 负雪明烛
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 日 —— 这么快就到双十一了??

最新文章

  1. Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制
  2. CentOS 7 vs CentOS 6的不同
  3. 實際案例: 已知要獲取臨時票証 (JsApi Ticket) 才能調用的接口
  4. 知方可补不足~sqlserver中使用sp_who查看sql的进程
  5. jquery插件-表单验证插件-rules
  6. DW与DM
  7. jquery之each()、$.each()[jQuery.each()]
  8. ThinkPHP之中的事务回滚
  9. Iframe跨域_ASP.NET
  10. 重学C++ (1)
  11. sort()排序
  12. Activity中setResult(int resultCode, Intent data)与onActivityResult(int requestCode, int resultCode, Intent data)方法的调用
  13. EasyUI基础searchbox&amp;amp;progressbar(搜索框,进度条)
  14. 第八十八节,html5+css3pc端固定布局,搜索区,插入大图,搜索框
  15. TCP状态转换
  16. Api管家系列(二):编辑和继承Class
  17. The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver
  18. 【转载】selenium之 定位以及切换frame(iframe)
  19. NodeJS开发环境搭建
  20. Check for Palindromes

热门文章

  1. kubernetes整个基础环境的准备
  2. php代码审计入门前必看
  3. ssm框架整合 — 更新完毕
  4. C语言中的字符和整数之间的转换
  5. 为 Rainbond Ingress Controller 设置负载均衡
  6. Shell $()、${}、$[]、$(())
  7. vue-baidu-map相关随笔
  8. day03 MySQL数据库之主键与外键
  9. 在JTable单元格上 加入组件,并赋予可编辑能力 [转]
  10. OC简单介绍