1. 程式人生 > >LeetCode 942. DI String Match

LeetCode 942. DI String Match

lar div similar color pre gmat ... nbsp return

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]

LeetCode 942. DI String Match