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;">

  • 相关阅读:
    PI SQL 语句
    MySQL数据库远程访问权限如何打开(两种方法)
    Ajax 实现无刷新分页
    php文件缓存方法总结
    PHP常用的缓存技术汇总
    PHP缓存机制详解
    用js判断页面刷新或关闭的方法
    基于JavaScript判断浏览器到底是关闭还是刷新(超准确)
    jquery刷新页面
    一些JavaScript基本函数
  • 原文地址:https://www.cnblogs.com/wxshi/p/7598533.html
Copyright © 2011-2022 走看看