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

  • 相关阅读:
    SRM146 DIV1 600
    SRM145 DIV1 1000
    SRM146 DIV1 300
    SRM145 DIV1 600
    【HTML打卡】0115 margin重叠、内联元素、css控制段落
    【HTML打卡】0114 盒模型margin、border、padding
    【HTML打卡】0113 div布局,css控制
    【HTML打卡】0112-html发展、doctype声明
    Machine Learning结课感想
    【ACM打卡】ZOJ 1045 2722 2830
  • 原文地址:https://www.cnblogs.com/wxshi/p/7598533.html
Copyright © 2011-2022 走看看