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 ...
随机推荐
- Bresenham快速画直线算法
现在的计算机的图像的都是用像素表示的,无论是点.直线.圆或其他图形最终都会以点的形式显示.人们看到屏幕的直线只不过是模拟出来的,人眼不能分辨出来而已.那么计算机是如何画直线的呢,其实有比较多的算法,这 ...
- mjpg-streamer移植
本文的copyright归yuweixian4230@163.com 所有,使用GPL发布,可以自由拷贝,转载.但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途. 作者:yuweix ...
- 侯捷STL学习(12)--STL相关内容hash+tuple
layout: post title: 侯捷STL学习(12) date: 2017-08-01 tag: 侯捷STL --- 第四讲 STL相关的内容 Hash Function 将hash函数封装 ...
- JAVA基础知识(16)-----Integer
Integer对象数字格式的字符串转成基本数据类型的方法:1:将该字符串封装成了Integer对象,并调用对象的方法intValue();2:使用Integer.parseInt(numstring) ...
- 解决CentOS遇到Qt编译(error: cannot find -lGL)
笔者CentOS 6.5 64位,安装完成Qt5.5.1.随意新建一个Qt Widgets Application. 结果遇到Qt编译问题,提示信息如下: error: cannot find -lG ...
- SqlServer——事务一编程进阶(SqlServer技术内幕 T-SQL程序设计 第九章
事务格式如下: 1.开启事务: begin tran 2.提交事务:commit tran 3.回滚事务:rollback tran 判断事务是提交还是应该回滚有两种方式,一是全局变量 @@error ...
- C#数据导出到Excel源代码
最近工作中需要做一个把用户信息作为excel导出的功能,就顺便整理搜集了一些常用的导出文件的源代码,以供以后不时之需,也希望能给大家带来一些方便 一.DataSet数据集内数据转化为Excel // ...
- jQuery实现按钮5秒后可以点击
废话少说,直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...
- MediaRecorder录像那些事
最近在做一个项目需要运用到MediaRecorder的API,之前都没接触过这部分,开始着手弄的时候各种各样的问题,真是让人崩溃呀! 最后通过网上的资料和大神的指点,当然也有自己几天坚持不懈的努力,终 ...
- SQL的发展史
在20世纪60年代,网状数据库系统(如CODASYL)和分层数据库系统(如IMS TM)是用于自动化银行业务.记帐和订单处理系统的一流技术,这些系统是由于商业大型计算机的引入才启用的.而SQL是在70 ...