当前位置:网站首页>520. 检测大写字母
520. 检测大写字母
2022-06-24 07:06:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:520. 检测大写字母
描述:我们定义,在以下情况时,单词的大写用法是正确的:
全部字母都是大写,比如 “USA” 。
单词中所有字母都不是大写,比如 “leetcode” 。
如果单词不只含有一个字母,只有首字母大写, 比如 “Google” 。
给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。
解题思路
1、顺序编程;
2、先判断第一个字母是大写还是小写,如果word[0]是小写,那么后面的必须都小写才能返回true;
3、如果word[0]是大写,还要分两种情况,看word[1]是大写还是小写。如果word[1]是大写,后面的必须也都是大写才返回true;如果word[1]是小写,后面也必须都是小写才返回true;
原代码##
class Solution {
public:
bool detectCapitalUse(string word) {
int len = word.size();
if (len == 1) return true;
if (word[0] >= 'a' && word[0] <= 'z') {
for (int i = 1; i < word.size(); ++i) {
if (word[i] >= 'A' && word[i] <= 'Z') return false;
}
return true;
} else {
if (word[1] >= 'A' && word[1] <= 'Z') {
for (int i = 1; i < word.size(); ++i) {
if (word[i] >= 'a' && word[i] <= 'z') return false;
}
return true;
} else {
for (int i = 1; i < word.size(); ++i) {
if (word[i] >= 'A' && word[i] <= 'Z') return false;
}
return true;
}
}
return true;
}
};
边栏推荐
- Redis cluster data skew
- WebRTC系列-网络传输之5选择最优connection切换
- 利用sonar做代码检查
- 解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
- Xtrabackup for data backup
- 工具类
- 提高INSERT速度
- How to replace the web player easyplayerproactivex Key in OCX?
- Easydss anonymous live channel data volume instability optimization scheme sharing
- How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
猜你喜欢
随机推荐
不能改变虚拟机电源状态报错解决方案
Redis cluster data skew
JS merge multiple objects and remove duplicates
xtrabackup做数据备份
Ordering of MySQL composite index
pymysql 向MySQL 插入数据无故报错
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
Scheduled database backup script
获取屏幕宽高工具类
There was an error checking the latest version of pip
leetcode 1268. Search suggestions system
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
mysql组合索引的有序性
IIS build wordpress5.7 manually
打印出来的对象是[object object],解决方法
一文详解|增长那些事儿
更改SSH端口号
Shell basic operators -- relational operators
2021-05-20computed和watch应用与区别
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思





![[untitled]](/img/94/792e8363dbfe67770e93b0dcdc8e72.png)



