[LC] 520. Detect Capital
2024-09-02 07:09:57
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:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- Only the first letter in this word is capital, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.
Example 1:
Input: "USA"
Output: True
Example 2:
Input: "FlaG"
Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
class Solution {
public boolean detectCapitalUse(String word) {
int numCap = 0;
int i = 0;
while (i < word.length()) {
if (Character.isUpperCase(word.charAt(i))) {
numCap += 1;
}
i += 1;
}
if (Character.isUpperCase(word.charAt(0))) {
return numCap == 1 || numCap == word.length();
}
return numCap == 0;
}
}
最新文章
- mysql高并发和表类型
- ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
- JS 面向对象随笔
- SQL Server 常用日期查询语句
- 一个简单的MySql数据库连接池的实现
- [ json editor] 如何在网页中使用Json editor 插件
- nanoTime对volatile 测试的一种写法
- 【COGS】894. 追查坏牛奶
- ubuntu 12 JDK 编译
- 括弧匹配检验(check)
- careercup-树与图 4.8
- IO调度算法研究1
- acm的第一场比赛的总结
- Java中Date日期字符串格式的各种转换
- Scala的安装,入门,学习,基础
- 【转】一文掌握 Linux 性能分析之网络篇
- 2018牛客暑期ACM多校训练营第一场(有坑未填)
- PI上导入RFC
- Java中sort实现降序排序
- bootstrapValidator常用验证规则总结