
题目描述
由范围 [0,n] 内所有整数组成的 n + 1 个整数的排列序列可以表示为长度为 n 的字符串 s ,其中:
    - 如果 perm[i] < perm[i + 1],那么s[i] == 'I'
- 如果 perm[i] > perm[i + 1],那么s[i] == 'D'
给定一个字符串 s ,重构排列 perm 并返回它。如果有多个有效排列perm,则返回其中 任何一个 。
 
示例 1:
输入:s = "IDID"
输出:[0,4,1,3,2]
示例 2:
输入:s = "III"
输出:[0,1,2,3]
示例 3:
输入:s = "DDI"
输出:[3,2,0,1]
 
提示:
    - 1 <= s.length <= 105
- s只包含字符- "I"或- "D"
解法
方法一:贪心
我们可以使用两个指针 low 和 high 分别表示当前的最小值和最大值,然后遍历字符串 s,如果当前字符是 I,那么我们就将 low 加入到结果数组中,并且 low 自增 1;如果当前字符是 D,那么我们就将 high 加入到结果数组中,并且 high 自减 1。
最后,我们将 low 加入到结果数组中,返回结果数组即可。
时间复杂度 \(O(n)\),空间复杂度 \(O(n)\)。其中 \(n\) 为字符串 s 的长度。
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13 | class Solution:
    def diStringMatch(self, s: str) -> List[int]:
        low, high = 0, len(s)
        ans = []
        for c in s:
            if c == "I":
                ans.append(low)
                low += 1
            else:
                ans.append(high)
                high -= 1
        ans.append(low)
        return ans
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16 | class Solution {
    public int[] diStringMatch(String s) {
        int n = s.length();
        int low = 0, high = n;
        int[] ans = new int[n + 1];
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'I') {
                ans[i] = low++;
            } else {
                ans[i] = high--;
            }
        }
        ans[n] = low;
        return ans;
    }
}
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17 | class Solution {
public:
    vector<int> diStringMatch(string s) {
        int n = s.size();
        int low = 0, high = n;
        vector<int> ans(n + 1);
        for (int i = 0; i < n; ++i) {
            if (s[i] == 'I') {
                ans[i] = low++;
            } else {
                ans[i] = high--;
            }
        }
        ans[n] = low;
        return ans;
    }
};
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14 | func diStringMatch(s string) (ans []int) {
    low, high := 0, len(s)
    for _, c := range s {
        if c == 'I' {
            ans = append(ans, low)
            low++
        } else {
            ans = append(ans, high)
            high--
        }
    }
    ans = append(ans, low)
    return
}
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13 | function diStringMatch(s: string): number[] {
    const ans: number[] = [];
    let [low, high] = [0, s.length];
    for (const c of s) {
        if (c === 'I') {
            ans.push(low++);
        } else {
            ans.push(high--);
        }
    }
    ans.push(low);
    return ans;
}
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20 | impl Solution {
    pub fn di_string_match(s: String) -> Vec<i32> {
        let mut low = 0;
        let mut high = s.len() as i32;
        let mut ans = Vec::with_capacity(s.len() + 1);
        for c in s.chars() {
            if c == 'I' {
                ans.push(low);
                low += 1;
            } else {
                ans.push(high);
                high -= 1;
            }
        }
        ans.push(low);
        ans
    }
}
 |