0
点赞
收藏
分享

微信扫一扫

leetcode 942. DI String Match [Python]

你带来了我的快乐 2022-02-17 阅读 39

以s的长度+1为最大数,以0为最小数。遍历s,遇到I,res贴最小数字,最小数+1,遇到D,贴最大数,最大数-1.

class Solution:
    def diStringMatch(self, s: str) -> List[int]:
        N = len(s)
        res = []
        small = 0
        big = N 
        for idx, char in enumerate(s):
            if char == 'I':
                res.append(small)
                small += 1
            elif char == 'D':
                res.append(big)
                big -= 1
        if s[-1] == 'D':
            res.append(big)
        elif s[-1] =='I':
            res.append(small)
        return res
举报

相关推荐

0 条评论