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

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

  • 相关阅读:
    P1135 奇怪的电梯
    pycharm设置快捷键在keymap下拉列表没有eclipse怎么办
    记录selenium简单实现自动点击操作
    selenium 批量下载文件,json,重命名
    python3.6+selenium使用chrome浏览器自动将文件下载到指定路径
    selenium + Java 设置文件默认下载路径
    详解介绍Selenium常用API的使用Java语言(完整版)
    Pycharm安装robot framework运行插件
    Python之robotframework+pycharm测试框架!
    基于Python3 Robot framework环境搭建
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6423760.html
Copyright © 2020-2023  润新知