美文网首页Leetcode
Leetcode 942. DI String Match

Leetcode 942. DI String Match

作者: SnailTyan | 来源:发表于2021-02-18 08:47 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

DI String Match

2. Solution

  • Version 1
class Solution:
    def diStringMatch(self, S):
        n = len(S)
        left = 0
        right = n
        result = []
        for i in range(n + 1):
            if i == n or S[i] == 'I':
                result.append(left)
                left += 1
            else:
                result.append(right)
                right -= 1
        return result
  • Version 2
class Solution:
    def diStringMatch(self, S):
        left = 0
        right = len(S)
        result = []
        for ch in S:
            if ch == 'I':
                result.append(left)
                left += 1
            else:
                result.append(right)
                right -= 1
        result.append(left)
        return result

Reference

  1. https://leetcode.com/problems/di-string-match/

相关文章

网友评论

    本文标题:Leetcode 942. DI String Match

    本文链接:https://www.haomeiwen.com/subject/hraxxltx.html