zoukankan      html  css  js  c++  java
  • LeetCode 520. Detect Capital

    520. Detect Capital

    Description Submission Solutions

    • Total Accepted: 2398
    • Total Submissions: 4301
    • Difficulty: Easy
    • Contributors: love_FDU_llp

    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. 用简单的判断来解决

    2. 用java自带的大小写转换方法和字符串的比较来解决

    3. 采用正则表达式来解决

    【java代码1】

    public class Solution {
        public boolean detectCapitalUse(String word) {
            if(word.length() == 0) return true;
            
            int state = -1;
            char[] array = word.toCharArray();
            if(array[0] >= 'a' && array[0] <= 'z') {
                state = 0;
            }
            else if(array[0] >= 'A' && array[0] <= 'Z') {
                state = 1;
            }
            else return false;
            
            for(int i = 1; i < array.length; i++) {
                switch(state) {
                    case 0:
                        if(array[i] >= 'a' && array[i] <= 'z') ;
                        else return false;
                    case 1:
                        if(array[i] >= 'a' && array[i] <= 'z') state = 0;
                        else if(array[i] >= 'A' && array[i] <= 'Z') state = 2;
                        break;
                    case 2:
                        if(array[i] >= 'A' && array[i] <= 'Z') ;
                        else return false;
                }
            }
            
            return true;
        }
    }

    【java代码2】

    public class Solution {
        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;
        }
    }

    【java代码3】

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

    虽然方法一代码比较长,但是效率比较高,正则表达式的匹配效率最低。简单的题不要出错。

  • 相关阅读:
    认识js运动
    BOM下的属性和方法---上
    BOM下的属性和方法---下
    鼠标跟随提示框
    [置顶] 关于CSDN2013博客之星的一些看法
    JSP内置对象---application
    C#中foreach语句的迭代器实现机制
    EBS动态创建账户组合实现
    稀里糊涂地被评为博客之星的候选人了,那就麻烦大家帮忙投个票吧~
    UNIX/Linux进程间通信IPC---管道--全总结(实例入门)
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6423760.html
Copyright © 2011-2022 走看看