字符串(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:

  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/命令式编程

 1 public class Solution {
 2     public boolean detectCapitalUse(String word) {
 3         char[] value = word.toCharArray();
 4         if(value.length == 1 || word == null) {
 5             return true;
 6         }
 7         boolean secIsUC = false;
 8         if(Character.isLowerCase(value[0])) {
 9             for(int i=1; i<value.length; i++) {
10                 if(Character.isUpperCase(value[i])) {
11                     return false;
12                 }
13             }
14         } else {
15             secIsUC = Character.isUpperCase(value[1]) ? true : false;
16             if(secIsUC) {
17                 for(int i=2; i<value.length; i++) {
18                     if(Character.isLowerCase(value[i])) {
19                         return false;
20                     }
21                 }
22             } else {
23                 for(int i=2; i<value.length; i++) {
24                     if(Character.isUpperCase(value[i])) {
25                         return false;
26                     }
27                 }
28             }
29         }
30         return true;
31     }
32 }

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.声明式编程

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

4.RegEx

1 public boolean detectCapitalUse(String word) {
2     return word.matches("[A-Z]+|[a-z]+|[A-Z][a-z]+");
3 }
时间: 2024-10-12 02:48:40

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

520. Detect Capital【easy】

520. Detect Capital[easy] 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 "U

520. 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 t

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 t

LeetCode 520. 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 t

[LeetCode&amp;Python] Problem 520. 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 t

[LC] 520. 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 t

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

R语言学习笔记:字符串处理

想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit" + month + ".jpg" VB:"fitbit" & month & ".jpg" Haskell:"fitbit" ++ month ++ ".jpg" 还想到concat之

R----stringr包介绍学习

目录 stringr介绍 stringr安装 stringr的API介绍 1. stringr介绍 stringr包被定义为一致的.简单易用的字符串工具集.所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理. 字符串处理虽然不是R语言中最主要的功能,却也是必不可少的,数据清洗.可视化等的操作都会用到.对于R语言本身的base包提供的字符串基础函数,随着时间的积累,已经变得很多地方不一致,不规范的命名,不标准的参数定义,很难看一眼就上手使用.字符串处理在其他语言中都