520. 检测大写字母
方法一:
class Solution:
def detectCapitalUse(self, word: str) -> bool:
l = len(word)
res = 0
index = -1
if l == 1:
return True
else:
for i in range(l):
if word[i].isupper():
res += 1
index = i
return res == l or res == 0 or (res == 1 and index == 0)
方法二:
class Solution:
def detectCapitalUse(self, word: str) -> bool:
n1 = 0
n2 = 0
l = len(word)
if word[0] >= "A" and word[0] <= "Z":
for i in range(1,l):
if word[i] >= "A" and word[i] <= "Z":
n1 += 1
else:
n2 += 1
if n1 == l - 1 or n2 == l - 1: #全大写或全小写
return True
else:
return False
else:
for i in range(1,len(word)):
if word[i] >= "A" and word[i] <= "Z":
return False
return True