以s的长度+1为最大数,以0为最小数。遍历s,遇到I,res贴最小数字,最小数+1,遇到D,贴最大数,最大数-1.
class Solution:
def diStringMatch(self, s: str) -> List[int]:
N = len(s)
res = []
small = 0
big = N
for idx, char in enumerate(s):
if char == 'I':
res.append(small)
small += 1
elif char == 'D':
res.append(big)
big -= 1
if s[-1] == 'D':
res.append(big)
elif s[-1] =='I':
res.append(small)
return res