字符串(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 ...
随机推荐
- linux防火墙(二)—— iptables语法之选项和控制类型
一.语法: iptables [-t 表名] 选项 [链名] [匹配条件] [-j 控制类型] 未指定表名时,默认用filter表:链名,控制类型要大写:除非设置默认策略,否则必须指定匹配条件:不指定 ...
- Opencv博文收藏列表
opencv识别二维码:https://blog.csdn.net/jia20003/article/details/77348170 opencv视频:http://www.opencv.org.c ...
- 一些很有意思的JS现象
关于JS对象的 . 和 [] []除了属性名可以比 .天马行空以外(比如我们要添加一个为'33-abc'的属性,一定得用[])),还有一个实际操作中的区别 Object.is的作用和两个奇特的现象 还 ...
- JavaScript的高级知识---词法分析
JavaScript的高级知识---词法分析 词法分析 词法分析方法: js运行前有一个类似编译的过程即词法分析,词法分析主要有三个步骤: 分析参数 再分析变量的声明 分析函数说明 函数在运行的瞬间, ...
- PHP 备份还原 MySql 数据库
原生 PHP 备份还原 MySql 数据库 支持 MySql,PDO 两种方式备份还原 php5.5 以上的版本建议开启pdo扩展,使用 pdo 备份还原数据 备份文件夹 db_backup.impo ...
- Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)
题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...
- tp5.0
入口文件绑定 : define('BIND_MODULE','admin/index'); 配置 auto_bind_moudle = ture|false. 入口自动绑定模块 入口文件 defin ...
- PHP二次开发
一.CMS类 如:phpcms.dedecms.empirecms.wordprss drupal joomla 二.shop类 如:ecshop eschopx shopnc emall mag ...
- 在Linux系统中,使用useradd命令新建用户后,登录该用户时shell开头为$,不显示用户名和路径,如下:
在~/.bash_profile中加入以下代码,若无该文件可自行创建: vi ~/.bash_profile #加入 #export PS1='[u@h W]$' 大写W代表最后路径,小写w代表详细路 ...
- Knime 连接 MYSQL 8
mysql8 腾空出世,话说mysql 跨过 6 7 版本直迈8,对这个数据库有跨时代的意思,引擎机制有个革命性的变革.决定尝试一把. 用大数据ETL工具Knime抽取数据.结果尴尬了: ERROR ...