Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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]
        

 

posted on 2018-12-18 13:18  Tonix  阅读(125)  评论(0编辑  收藏  举报