-
Notifications
You must be signed in to change notification settings - Fork 12
/
484. Find Permutation.cpp
59 lines (47 loc) · 1.5 KB
/
484. Find Permutation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Link: https://leetcode.com/problems/find-permutation/
A permutation perm of n integers of all the integers in the range [1, n] can be represented as a string s of length n - 1 where:
s[i] == 'I' if perm[i] < perm[i + 1], and
s[i] == 'D' if perm[i] > perm[i + 1].
Given a string s, reconstruct the lexicographically smallest permutation perm and return it.
Example 1:
Input: s = "I"
Output: [1,2]
Explanation: [1,2] is the only legal permutation that can represented by s, where the number 1 and 2 construct an increasing relationship.
Example 2:
Input: s = "DI"
Output: [2,1,3]
Explanation: Both [2,1,3] and [3,1,2] can be represented as "DI", but since we want to find the smallest lexicographical permutation, you should return [2,1,3]
Constraints:
1 <= s.length <= 10^5
s[i] is either 'I' or 'D'.
*/
// solved by Milon
class Solution {
public:
vector<int> findPermutation(string s) {
int sz = s.size();
vector<int> ans(sz + 1);
for (int i = 0; i <= sz; i++)
ans[i] = i+1;
int num = 1;
for (int i = 0; i < sz; i++) {
if (s[i] == 'I')
continue;
else {
int j = i;
while (j < sz && s[j] == 'D') {
j++;
}
int last = j;
while (i < j) {
swap(ans[i], ans[j]);
i++;
j--;
}
i = last;
}
}
return ans;
}
};