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

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

  • 相关阅读:
    无法导入panda包解决方法
    VUE学习笔记——基础标签,函数
    jobs指令man手册内容
    linux 部分参数的全名
    bilibiliUP数据爬取——requests库与jason库运用实例
    宝塔面板无法进入phpadmin管理数据库解决办法
    python-spider_BeautifulSoup入门实践(一)安装以及简单的抓取数据
    c++程序设计实践——银行系统
    opencv-学习笔记
    关于pipeline的一篇转载博文https://www.cnblogs.com/midhillzhou/p/5588958.html
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6423760.html
Copyright © 2011-2022 走看看