当前位置:网站首页>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;
}
};
边栏推荐
- Jenkins is deployed automatically and cannot connect to the dependent service [solved]
- Mysql数据(Liunx环境)定时备份
- WebRTC系列-网络传输之5选择最优connection切换
- Get screen width and height tool class
- IIS build wordpress5.7 manually
- orb slam build bug: undefined reference to symbol ‘_ZN5boost6system15system_categoryEv‘
- 5 minutes, excellent customer service chat handling skills
- Summary of methods in numpy
- 【NOI模拟赛】寄(树形DP)
- rsync做文件备份
猜你喜欢

一文详解|增长那些事儿

5 minutes, excellent customer service chat handling skills

分布式 | 如何与 DBLE 进行“秘密通话”

表单图片上传在Chorme中无法查看请求体的二进制图片信息

關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL

Jenkins自动化部署,连接不到所依赖的服务【已解决】

中国芯片独角兽公司

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

WebRTC系列-网络传输之5选择最优connection切换
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management
随机推荐
Common CVM transcribes audio using virtual sound card
String转Base64
JUC personal simple notes
一文详解|增长那些事儿
Common misconceptions in Tencent conference API - signature error_ code 200003
【生活思考】计划与自律
Xiaohei ai4code code baseline nibble 1
rsync做文件备份
K8S部署高可用postgresql集群 —— 筑梦之路
Scheduled database backup script
Shell basic operators -- relational operators
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
Summary of methods in numpy
相机投影矩阵计算
How to mount a USB hard disk with NTFS file format under RHEL5 system
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
5 minutes, excellent customer service chat handling skills
一文讲透,商业智能BI未来发展趋势如何
Picture tools
2022春招面试总结