forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
50 lines (38 loc) · 914 Bytes
/
main2.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
/// Source : https://leetcode.com/problems/di-string-match/
/// Author : liuyubobobo
/// Time : 2018-11-17
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Generating the result from min value 0 and max value n - 1
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> diStringMatch(string S) {
int n = S.size();
vector<int> res(n + 1, 0);
int l = 0, h = n;
for(int i = 0; i < S.size(); i ++){
if(S[i] == 'I')
res[i] = l ++;
else
res[i] = h --;
}
assert(l == h);
res.back() = l;
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
string S1 = "IDID";
print_vec(Solution().diStringMatch(S1));
return 0;
}