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

  • 相关阅读:
    php入门变量
    php入门引言
    帝国cms7.0修改默认搜索模版中的分页[!--show.page--]
    帝国cms 列表页分页样式修改美化【2】
    划词翻译
    MySQL几种常见的排序方式
    你的眼界决定了你的格局
    Easy Window Switcher支持Windows 10虚拟桌面
    鬼谷子的人生智慧
    接口设计的八大黄金规则
  • 原文地址:https://www.cnblogs.com/wxshi/p/7598533.html
Copyright © 2011-2022 走看看