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 ...
随机推荐
- calc PI
https://en.wikipedia.org/wiki/Pi code https://github.com/HHS-IntroProgramming/Calculate-Pi https://g ...
- Celery-4.1 用户指南: Calling Tasks(调用任务)
基础 本文档描述 Celery 中任务实例和 Canvas 使用的统一 “Calling API”. API 中定义了一个执行选项的标准集,以及三个方法: - apply_async(args[, k ...
- python-xlrd api
1.导入模块 import xlrd from xlrd import open_workbook 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFi ...
- 第五章 深入class文件结构(待续)
JVM指令集简介 class文件头的表示形式 常量池 类信息 Fields和Methods定义 类属性描述 Javap生成的class文件结构
- 【坑】idea+tomcat
idea用tomcat外置容器部署debug要清掉webapp里面的同名项目,否则就呵呵呵呵了
- oracle DCL-(grant、revoke )
1.授权GRANT <权限列表> to <user_name>; 2.收回权限REVOKE <权限列表> from <user_name>
- linq to sql 不能更新的问题
今天在项目中用linq更新一个表的时候,结果怎么都更新不了,最蛋疼的是什么异常也不报,发现db.table1.isReadOnly为True 知道问题所在,百度后得到解决办法: 原来是我的表没有增加主 ...
- SpringBoot06 统一响应格式
1 要求 每个请求成功后,后台返回的响应格式都是一致的,例如: 2 创建一个视图模型 该模型用于格式化响应数据 package cn.xiangxu.springboottest.model.data ...
- linux时区修改为中国时区
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- Luogu 1445 樱花
BZOJ 2721 唔,太菜了弄不来. 先通分:得到 $\frac{x + y}{xy} = \frac{1}{n!}$ 两边乘一下 $(x + y)n! - xy = 0$ 两边加上$(n!)^2$ ...