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 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

很简单的一道题,渣渣如我只能暴力求解:

1.brutal force/命令式编程

 public class Solution {
public boolean detectCapitalUse(String word) {
char[] value = word.toCharArray();
if(value.length == 1 || word == null) {
return true;
}
boolean secIsUC = false;
if(Character.isLowerCase(value[0])) {
for(int i=1; i<value.length; i++) {
if(Character.isUpperCase(value[i])) {
return false;
}
}
} else {
secIsUC = Character.isUpperCase(value[1]) ? true : false;
if(secIsUC) {
for(int i=2; i<value.length; i++) {
if(Character.isLowerCase(value[i])) {
return false;
}
}
} else {
for(int i=2; i<value.length; i++) {
if(Character.isUpperCase(value[i])) {
return false;
}
}
}
}
return true;
}
}

2.暴力求解升级版

1 public class Solution {
2 public boolean detectCapitalUse(String word) {
3 int cnt = 0;
4 for(char c: word.toCharArray()) if('Z' - c >= 0) cnt++;
5 return ((cnt==0 || cnt==word.length()) || (cnt==1 && 'Z' - word.charAt(0)>=0));
6 }
7 }

3.声明式编程

 public boolean detectCapitalUse(String word) {
if (word.length() < 2) return true;
if (word.toUpperCase().equals(word)) return true;
if (word.substring(1).toLowerCase().equals(word.substring(1))) return true;
return false;
}

4.RegEx

 public boolean detectCapitalUse(String word) {
return word.matches("[A-Z]+|[a-z]+|[A-Z][a-z]+");
}

字符串(1)——Detect Capital的更多相关文章

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

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

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

  3. LeetCode——Detect Capital

    LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...

  4. 520. Detect Capital【easy】

    520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...

  5. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  6. 520. Detect Capital

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

  7. 520. Detect Capital判断单词有效性

    [抄题]: Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  8. 【LeetCode】520. Detect Capital 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...

  9. Detect Capital

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

随机推荐

  1. luoguP2418 yyy loves OI IV

    https://www.luogu.org/problemnew/show/P2418 暴力 DP 做这题只有 30 分 考虑用线段树优化这个 DP 先处理一下整个房间都膜拜一个人的情况,然后将 1 ...

  2. vmware vSphere虚拟网络之标准交换机(二)

    一.标准交换机的特点: 1)只能用于物理主机 2)其他主机不能共享同一个虚拟交换机 3)不具备任何灵活性 4)每台ESXi主机都要配置一遍 二.网络图: 三.创建标准交换机: 登录web vCente ...

  3. Java中的Random()函数-----转载

    Java中的Random()函数 (2013-01-24 21:01:04) 转载▼ 标签: java random 随机函数 杂谈 分类: Java 今天在做Java练习的时候注意到了Java里面的 ...

  4. 杀死进程命令 kill

    一般kill命令和ps命令结合使用, 例:现在想杀死telnet的进程 1.在所有进程中查看telnet命令 ps -ef |grep telnet 2.根据上面命令查到的进程id,如pid 是 xx ...

  5. ob系列函数归纳

     输出控制函数(output control函数) flush — 刷新输出缓冲ob_clean — 清空(擦掉)输出缓冲区ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲ob_end_ ...

  6. Qt 学习之路 2(51):布尔表达式树模型

    Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...

  7. mysql 表的分区

    如何判断当前MySQL是否支持分区 命令:show variables like '%partition%' 运行结果: mysql> show variables like '%partiti ...

  8. Python之逻辑运算符

    这一小节我在动笔之前犹豫到底要不要动手写,虽然简单但是防止遗忘,博主还是决定记录一下.Python中运算符主要分为算术运算符,赋值运算符,比较运算符,逻辑运算符以及成员运算符.下面详细记录这5种运算符 ...

  9. LeetCode6. Z字形变换

    描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  10. abp + angular 前端使用 hash ,登录界面不跳转问题

    abp 项目默认的路由没有使用hash,这会导致手动刷新浏览器时,页面404错误: 解决方法网上很多,就是在路由里添加一个{useHash: true},就行了. #用Hash带来的新问题# abp框 ...