LeetCode--Python解析【Detect Capital】(520)

题目:

方法:

class Solution:
    def detectCapitalUse(self, word):
        """
        :type word: str
        :rtype: bool
        """
        test = word.upper()
        test1 = word.lower()
        if test == word or test1 == word:
            return True
        for i in range(1,len(word)):
            if word[i] == test[i]:return False
        return True

首先转换为全部大写和全部小写,如果相等的话返回True

然后从第二个字符开始,与转换为全部大写的字符串开始比价

如过有相等的元素出现则返回False

猜你喜欢

转载自blog.csdn.net/ZJRN1027/article/details/81187342
今日推荐