leetCode Detect Capital
1、问题描述
Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
1.All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode".
2.Only the first letter in this word is capital if it has more than one letter, like "Google".
3.Otherwise, we define that this word doesn't use capitals in a right way.
检测一个string中的大写字母的对错,以下三种情况中,大写字母的格式是正确的。
1. 整个string全部是大写字母。
2.整个string全是小写字母。
3.单词中首字母大写。
2、问题分析
首先处理特殊情况,输入的string 长度是0 或者 1的时候,返回 true。
然后根据 前两个字母的大小分析。
1 bool detectCapitalUse(string word) 2 { 3 if( word.size() == 0 || word.size() == 1 ) 4 return true; 5 6 if( islower( word[0] ) ) 7 { 8 for(int i = 1 ; i < word.size(); i++) 9 { 10 if( isupper(word[i]) ) 11 return false; 12 } 13 } 14 15 if( isupper( word[0] ) && isupper(word[1]) ) 16 { 17 for( int i = 2; i < word.size(); i++) 18 if( islower( word[i]) ) 19 return false; 20 } 21 22 if( isupper(word[0]) && islower(word[1]) ) 23 { 24 for(int i = 2; i< word.size(); i++) 25 if(isupper(word[i])) 26 return false; 27 } 28 29 return true; 30 }
1. 如果第一个字母是小写,那么后续字母中出现 大写字母,就返回false.
2.如果前两个字母都是大写,那么后续的字母再出现小写字母就返回false。
3.如果第一个字母是大写,第二个字母是小写,那么后续再出现大写字母,就返回false。
3、代码
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具