感觉不是很难的题,控制一下变量和范围就OK了。
class Solution(object): def diStringMatch(self, S): """ :type S: str :rtype: List[int] """ x1 = 10000 x2 = 10000 ret = [10000] for i in S: if i == 'I': x1 += 1 ret.append(x1) else: x2 -= 1 ret.append(x2) x = min(ret) #print ret, x ret = [i - x for i in ret] return ret