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

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

  • 相关阅读:
    获取随机数
    性能测试工具
    Oracle 级联删除
    一些shell用法
    英文
    主题:【元宵赏灯】蛇年杭州元宵赏灯攻略(上城区、滨江区、下城区)
    CListCtrl 列表选中项非焦点时也是藍色
    ASCII码表
    杭州市公积金提取及相关知识
    ListBox设置水平滚动条
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6423760.html
Copyright © 2011-2022 走看看