[LeetCode]#942. DI String Match
Environment: Python 3.7
Key technique: append function
Given a string
S that only contains "I" (increase) or "D" (decrease), let
N = S.length.
Return any permutation
[0, 1, ..., N] such that for all
i = 0, ..., N-1:
S[i] == "I", then
A[i] < A[i+1]
S[i] == "D", then
A[i] > A[i+1]
- Set low=0 and high is 4 based on S.
- If S[i] is “I”, add low to ans list and low add 1.
- If S[i] is “D”, add high to ans list and high decrease 1.
- The last list you can add low and high are OK.
def diStringMatch(self, S):
for i in S:
return ans +[low]