Python教程

leetcode 520. 检测大写字母 python

本文主要是介绍leetcode 520. 检测大写字母 python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目描述:

 题解:

1.遍历输入字符串word的每一个字符,将其中的大写字母位置记录在pos中。

2.判断pos:

<1>如果len(pos)和word长度相同,说明是全部大写的情况。

<2>如果len为空,对应全部小写的情况。

<3>如果len长度为1,并且对应的位置序号为0,对应首字母大写的情况。

其他情况全部返回False。

class Solution(object):
    def detectCapitalUse(self, word):
        pos = []
        for i in range(len(word)):
            if word[i]>='A' and word[i]<='Z':
                pos.append(i)
        if len(pos)==len(word):
            return True
        elif len(pos)==0:
            return True
        elif len(pos)==1 and pos[0]==0:
            return True
        return False

 在leetcode评论区看到的绝妙解答:

 

这篇关于leetcode 520. 检测大写字母 python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!