字符串(1)——Detect Capital
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 1:
Input: "USA"
Output: TrueExample 2:
Input: "FlaG"
Output: FalseNote: 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的更多相关文章
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 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——Detect Capital
LeetCode--Detect Capital Question Given a word, you need to judge whether the usage of capitals in i ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the ...
- 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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...
- Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- 洛谷P2510 [HAOI2008]下落的圆盘(计算几何)
题面 传送门 题解 对于每个圆,我们单独计算它被覆盖的周长是多少 只有相交的情况需要考虑,我们需要知道相交的那段圆弧的角度,发现其中一个交点和两个圆的圆心可以构成一个三角形且三边都已经知道了,那么我们 ...
- webstorm激活服务器地址
2017.1.4版本可用 http://idea.imsxm.com/
- SHELL编程之条件判断
一.if 语句结构 (1)单分支语句结构 if 条件测试操作 then 命令序列 fi #!/bin/bash MOUNT_DIR="/media/cdrom/" #-d $M ...
- SDUT OJ 图结构练习——最短路径 ( Floyed 算法 AND Dijkstra算法)
图结构练习——最短路径 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- width:100%以什么为基准的测试
起初是遇到这样一个问题:当盒模型设为box-sizing:border-box;(移动端上经常这么干).子盒子的width:100%,子盒子的width等于父盒子contend的长度还是condend ...
- UISearchBar 自定义处理
首先通过 KVC 获取到内部的 textField, 然后自定制处理 UITextField *searchField = [searchBar valueForKey:@"searchFi ...
- shell-003:用for循环统计内存使用量
shell-100主要是用于练习! #!/bin/bash # 统计内存的使用量(这里用ps统计) # 第一步:不打印第一行,这里的sed ‘1d’ 去掉 for n in `ps aux |sed ...
- 使用Swagger生成简单接口文档
使用swagger通过简单的配置可以生成简单的接口文档: 依赖包: // Swagger2 compile 'io.springfox:springfox-swagger2:2.8.0' compil ...
- C++_函数1-编程的基本模块函数
以下是<C++ Primer Plus>中第七章的内容: 使用C++函数的3个步骤: 提供函数定义 提供函数原型 调用函数 7.1.1 定义函数 函数分成两类:没有返回值的函数.有返回值的 ...
- 113th LeetCode Weekly Contest Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...