LeetCode 942. DI String Match

A simple greedy construction procedure:

- Fill out the seq of 'I' with 0, 1, 2, 3...

- Similarly, for the seq of 'D', fill it out with n, n - 1, n - 2...

- At last, don't forget to put last element

class Solution(object):
    def diStringMatch(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        a, b = 0, len(S)
        r = []
        for c in S:
            if c == 'I':
                r += [a]
                a += 1
            else:
                r += [b]
                b -= 1
        return r + [a]
        

猜你喜欢

转载自www.cnblogs.com/tonix/p/10136521.html