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]+");
        }
    }

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

  • 相关阅读:
    Select * 一定不走索引是否正确?
    csshack技术
    ios学习笔记之UIViewControl生命周期
    selenium webdriver (python)
    string中Insert与Format效率对比、String与List中Contains与IndexOf的效率对比
    HDU 2083 简易版之最短距离
    xtrabackup支持的engine
    C++可变参数的另一种实现
    程序员应具备的素质(国内的大多程序员生产力不够,所以只能早早转行)
    Qt导出Excel的简单实现
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6423760.html
Copyright © 2011-2022 走看看