码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 942. DI String Match

时间:2018-12-18 13:59:44      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:type   roc   nbsp   put   get   element   leetcode   pre   ret   

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

标签:type   roc   nbsp   put   get   element   leetcode   pre   ret   

原文地址:https://www.cnblogs.com/tonix/p/10136521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!