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. 【语义分割】PSPNet中PSP模块的pytorch实现

    github地址:https://github.com/Lextal/pspnet-pytorch/blob/master/pspnet.py PSP模块示意图如下 代码如下 class PSPMod ...

  2. CSS3 选择器 修改 整数个样式

    .blogbottom ul li:nth-child(4n){margin-right:0px;} 说明:4n就是每第4个.

  3. window 系统 cygwin swool 问题

    cygwin 终端乱码  端口占用 查看 tcp  端口: netstat -tno  或者 netstat -an | grep 端口 杀死进程号 : kill   进程号 --->  143 ...

  4. linux 查看当前目录下包含某个字符串的文件

    $ grep -rn '字符串' 很好用~

  5. POJ_1019 Number Sequence 【递推】

    题目: A single positive integer i is given. Write a program to find the digit located in the position ...

  6. 使用xcode测量ios8.1机型时的项目兼容问题

    打开xcode,创建一个新项目 点击左上角的三角形打开模拟器,打开模拟器中的safari,把项目链接输入,即可测试 下面为切换机型的方法:

  7. sqlmap命令小结

    --technique 这个参数可以指定sqlmap使用的探测技术,默认情况下会测试所有的方式. 支持的探测方式如下: B: Boolean-based blind SQL injection(布尔型 ...

  8. 【网络】EIGRP负载均衡、手工汇总、泄露明细、安全认证

    基于6.6.6.6/24访问13.1.1.1/24进行负载均衡实例图 一.等价负载均衡 等价负载均衡前,R5路由器背后的6.6.6.6/24访问R1的13.1.1.1/24, 从15.1.1.0/24 ...

  9. C++ GUI Qt4编程(10)-3.4spreadsheet

    1. C++ GUI Qt4编程第三章,增加spreadsheet. 2. spreadsheet.h /**/ #ifndef SPREADSHEET_H #define SPREADSHEET_H ...

  10. server 2012 R2查询端口

    1. win+r弹出运行对话框,输入cmd,打开cmd窗口 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 ...