zoukankan      html  css  js  c++  java
  • 520. Detect Capital


     

    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.全部为小写字母,3.第一个字母为大写字母。判断给定word是否满足其中一项规则
    该问题可以进行转化为:1.word大写字母个数为字符串长度,2.大写字母个数为0,3.大写字母个数为1且为第一位
    class Solution {
        public boolean detectCapitalUse(String word) {
            int cnt = 0;
            for(char c:word.toCharArray())  
                if(c <= 'Z')         //大写
                    cnt ++;
            if((cnt == 0|| cnt==word.length()) || (cnt==1 && word.charAt(0) <= 'Z'))
               return true;
            return false;
        }
    }
     
     

    <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

  • 相关阅读:
    EL表达式(Expression Language)
    JSP简单功能介绍
    MySQL基础使用
    JDBC
    MySQL安装卸载
    stanfordnlp dependencies(依存关系简写表)
    不需要深度学习就能做的文本分类器
    词向量的cbow模型
    pytorch实现自己的textCNN
    OpenCV编译viz模块
  • 原文地址:https://www.cnblogs.com/wxshi/p/7598533.html
Copyright © 2011-2022 走看看