- 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 True
if 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;
}
}



