zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 520 检测大写字母

    520. 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确。

    我们定义,在以下情况时,单词的大写用法是正确的:

    全部字母都是大写,比如"USA"。
    单词中所有字母都不是大写,比如"leetcode"。
    如果单词不只含有一个字母,只有首字母大写, 比如 “Google”。
    否则,我们定义这个单词没有正确使用大写字母。

    示例 1:

    输入: “USA”
    输出: True
    示例 2:

    输入: “FlaG”
    输出: False
    注意: 输入是由大写和小写拉丁字母组成的非空单词。

    class Solution {
        public boolean detectCapitalUse(String word) {
                int len = word.length();
                int cap = 0;
                for(int i=0; i<len; i++){
                    char c = word.charAt(i);
                    if(c >= 'A' && c <= 'Z') cap++;
                }
    
                if(cap == len || cap == 0) return true;
                char f = word.charAt(0);
                if(cap == 1 && (f >= 'A') && (f <= 'Z')) return true;
                return false;
        }
    }
    
  • 相关阅读:
    Mix and Build(简单DP)
    Is It A Tree?(并查集)
    Paths on a Grid(简单组合数学)
    Code(组合数学)
    Round Numbers(组合数学)
    Inviting Friends(二分+背包)
    Communication System(dp)
    Human Gene Functions
    Pearls
    敌兵布阵(线段树HDU 1166)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12946411.html
Copyright © 2011-2022 走看看