Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串)
题目描述
已知一个单词,你需要给出它是否为"大写词"
我们定义的"大写词"有下面三种情况:
- 所有字母都是大写,比如"USA"
- 所有字母都不是大写,比如"leetcode"
- 只有第一个字母是大写,比如"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;
}
};
Leetcode 520. Detect Capital 发现大写词 (字符串)的更多相关文章
- 50. leetcode 520. Detect Capital
520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or ...
- LeetCode 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- LeetCode: 520 Detect Capital(easy)
题目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define t ...
- LeetCode - 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- 【LeetCode】520. Detect Capital 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...
- [LeetCode] Detect Capital 检测大写格式
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- 开发环境入门 linux基础 (部分) 复制 用户和组操作 权限更改
复制 用户和组操作 权限更改 CP 复制命令 cp 源文件 目标文件 a) –r(recursive,递归的):递归地复制目录.当复制一个目录时,复制该目录中所有的内容,其中包括子目录的全部内容. b ...
- js实现大文件分片上传的方法
借助js的Blob对象FormData对象可以实现大文件分片上传的功能,关于Blob和FormData的具体使用方法可以到如下地址去查看FormData 对象的使用Blob 对象的使用以下是实现代码, ...
- DAY13-前端之JavaScript
JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名ScriptEase(客 ...
- Python单例模式剖析
在聊这之前我们首先要明确的是,单例模式在实际中的意义以及在python中具有实现的价值? 当前,相信有很多人支持单例模式,也有不少人反对,尤其是在python中,目前依旧具有很大的争议性.我们要在评论 ...
- 关于Bundle对象的思考
在开发过程中,我们经常使用bundle对象来携带二进制数据,通过INTENT传递出去,那么BUNDLE对象到底是什么?其结构如何? 简要来说,bundle对象类似于一个map,内部是通过<key ...
- [cerc2017J]Justified Jungle
题目大意:删去k条边,树变为相等个点的连通分量,求所有正整数k. 解题关键:树dp,不必求因子. #include<bits/stdc++.h> using namespace std; ...
- cocos2d-js 热更新模块 使用AssetsManager
原帖子地址:http://cn.cocos2d-x.org/tutorial/show?id=1186 在这个文章中原作者已经说的很清楚,我在这个其他改动一些适用我项目中需求 1.满足Web和Nati ...
- 使用RSS提升DPDK应用的性能(转)
本文描述了RSS以及在DPDK中如何配置RSS达到性能提升和统一分发. 什么是RSS RSS(Receive Side Scaling)是一种能够在多处理器系统下使接收报文在多个CPU之间高效分发的网 ...
- 使用Notepad++运行Python脚本
1.安装python,我用的是anaconda 2.打开找到anaconda安装目录,找到python.exe,记录绝对路径.我的是D:\app\anaconda3\python.exe 3.Note ...
- Android ExpandableListView的使用
一.MainActivity要继承ExpandableListActivity.效果是当单击ListView的子项是显示另一个ListView. package com.example.explear ...