力扣每日一题2021-11-13检测大写字母
检测大写字母
- 520.检测大写字母
- 题目描述
- 思路:模拟
- Python实现
- Java实现
520.检测大写字母
题目描述
检测大写字母
思路:模拟
根据题意对三种状态分别匹配即可。
Python实现
class Solution:def detectCapitalUse(self, word: str) -> bool:# 模拟if str.islower(word[0]):return str.islower(word)if len(word) <= 2:return Trueif str.islower(word[1]):return str.islower(word[2:])return str.isupper(word)
Java实现
class Solution {public boolean detectCapitalUse(String word) {// 模拟if (Character.isLowerCase(word.charAt(0))) {for (int i=1; i < word.length(); i++) {if (!Character.isLowerCase(word.charAt(i))) return false;}return true;}if (word.length() <= 2) {return true;}if (Character.isLowerCase(word.charAt(1))) {for (int i=2; i < word.length(); i++) {if (!Character.isLowerCase(word.charAt(i))) return false;}return true;}for (int i=2; i < word.length(); i++) {if (Character.isLowerCase(word.charAt(i))) return false;}return true;}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
