zoukankan      html  css  js  c++  java
  • LeetCode——Detect Capital

    LeetCode——Detect Capital

    Question

    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:

    All letters in this word are capitals, like "USA".
    All letters in this word are not capitals, like "leetcode".
    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.

    Answer

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            if (word.length() == 1)
                return true;
    
            char c  = word[0];
            if (c >= 'a' && c <= 'z') {
                for (int i = 1; i < word.length(); i++) {
                    if (word[i] < 'a' || word[i] > 'z')
                        return false;
                }
                return true;
            } else {
                c = word[1];
                if (c >= 'A' && c <= 'Z') {
                    for (int i = 2; i < word.length(); i++) {
                        if (word[i] < 'A' || word[i] > 'Z')
                            return false;
                    }
                    return true;
                } else {
                    for (int i = 2; i < word.length(); i++) {
                        if (word[i] < 'a' || word[i] > 'z')
                            return false;
                    }
                    return true;
                }
            }
        }
    };
    
  • 相关阅读:
    三、ADO.Net基础【04】简单查询
    Canvas 图片绕边旋转的小动画
    Canvas 剪切图片
    Canvas 图片平铺设置
    Canvas 给图形绘制阴影
    Canvas 图形组合方式
    [转]JS获取URL传参方法
    HTML5 FileReader接口学习笔记
    css3实现圆角边框渐变
    HTML5新增属性学习笔记
  • 原文地址:https://www.cnblogs.com/zhonghuasong/p/6658633.html
Copyright © 2011-2022 走看看