• Leetcode 520. Detect Capital 发现大写词 (字符串)


    Leetcode 520. Detect Capital 发现大写词 (字符串)

    题目描述

    已知一个单词,你需要给出它是否为"大写词"
    我们定义的"大写词"有下面三种情况:

    1. 所有字母都是大写,比如"USA"
    2. 所有字母都不是大写,比如"leetcode"
    3. 只有第一个字母是大写,比如"Google"

    测试样例

    Input: "USA"
    Output: True
    
    Input: "FlaG"
    Output: False
    

    详细分析

    水题,按照上面定义的三种情况写代码即可。
    稍微值得注意的是如果字符串为空输出是true

    代码实现

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            if (word.length() == 0) {
                return true;
            }
            bool capitalFirst = false;
            int capitalCnt = 0;
            if (isupper(word[0])) {
                capitalFirst = true;
                capitalCnt++;
            }
            for (int i = 1; i < word.length(); i++) {
                if (isupper(word.at(i))) {
                    capitalCnt++;
                }
            }
            if (capitalCnt == 0 ||
                capitalCnt == word.length() ||
                (capitalFirst && capitalCnt == 1)) {
                return true;
            }
            return false;
        }
    };
    
  • 相关阅读:
    HTML介绍
    python D41 前端初识
    mysql索引原理与查询优化
    python D41
    python D40 pymsql和navicat
    python D40 以及多表查询
    python D35 selectors模块
    python D35 I/O阻塞模型
    测试过程
    测试基础
  • 原文地址:https://www.cnblogs.com/ysherlock/p/8467034.html
Copyright © 2020-2023  润新知