Leedcode-檢測大寫字母

Junior_bond發表於2024-05-31

自己寫的:

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        n=len(word)
        count=0
        for i in word:
            if self.is_upper(i):
                count+=1
        if count==0:
            return True
        elif count==n:
            return True
        elif self.is_upper(word[0]) and count==1:
            return True
        else:
            return False
    def is_upper(self,char):
        if not (ord('A')<=ord(char)<=ord('Z')):
            return False
        else:
            return True

相關文章