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

    思路:将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,
    否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,
    否则返回false。
    public boolean detectCapitalUse(String word) {
            int len=word.length();
            String up = word.toUpperCase();  
            String low = word.toLowerCase();  
            if (word.equals(up) || word.equals(low))  
                return true;  
             String last = word.substring(1, len);  
            if (last.toLowerCase().equals(last))  
               return true;  
             return false;  
        }




  • 相关阅读:
    (16)C#继承
    (3)新概念英语一(11-20)lessons
    (15)C#集合
    (14)编码
    (2)新概念英语一(1-10)lessons
    (13)C#数组和元组
    Uva 11729 Commando War
    Uva 11292 Dragon of Loowater
    谈谈单元最短路
    [USACO 3.3.1]骑马修栅栏t
  • 原文地址:https://www.cnblogs.com/sunli0205/p/6430728.html
Copyright © 2011-2022 走看看