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.

    题目含义:给定一个词,你需要判断它中的大小写使用是否正确。当以下情况之一存在时,我们将单词中大小写的使用定义为正确:这个单词中的所有字母都是大写,如“USA”。这个单词中的所有字母都不是大写,例如“leetcode”。只有此字中的第一个字母是大写,如果它有多个字母,如“Google”。否则,我们定义这个词不以正确的方式使用大写。

    思路:找出所有大写字母的个数captialCount,与字符串长度做比较,都是小写字母、都是大写字母、只有首字母是大写字母的,都返回true。判断公式为:

    captialCount == 0 || captialCount == word.length() || (captialCount == 1 && ('Z' - word.charAt(0) >= 0))
    字母    ASCII码      十进制数
    0         00110000      48
    9           00111001        57
    A          01000001         65
    Z          01011010         90
    a         01100001         97
    z          01111010         122
    DEL (delete)   01111111   127
    1     public boolean detectCapitalUse(String word) {
    2         char[] words = word.toCharArray();
    3         int captialCount = 0;
    4         for (char letter : words) {
    5             if ('Z' - letter >= 0) captialCount++;
    6         }
    7 return captialCount == 0 || captialCount == word.length() || (captialCount == 1 && ('Z' - word.charAt(0) >= 0)); 8 }
  • 相关阅读:
    学习html5 中的canvas(一)
    js中的width问题
    css3中我们不知道的一些属性
    css3的渐变效果
    校园商铺-6店铺编辑列表和列表功能-1店铺信息编辑之Dao层开发
    校园商铺-4店铺注册功能模块-14前后端联调技巧细化与总结
    校园商铺-4店铺注册功能模块-13前后端联调验证整体模块功能
    校园商铺-4店铺注册功能模块-12引入kaptcha实现验证码
    11-接下来如何做
    10-K最近邻算法
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7680279.html
Copyright © 2011-2022 走看看