File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
0942-DI-String-Match/cpp-0942 Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.12 )
2
+ project (C )
3
+
4
+ set (CMAKE_CXX_STANDARD 11 )
5
+
6
+ add_executable (C main2.cpp )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/di-string-match/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-11-17
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+
8
+ using namespace std ;
9
+
10
+
11
+ // / Generating the result from 0
12
+ // / Adding the offset at last
13
+ // /
14
+ // / Time Complexity: O(n)
15
+ // / Space Complexity: O(1)
16
+ class Solution {
17
+ public:
18
+ vector<int > diStringMatch (string S) {
19
+
20
+ int n = S.size ();
21
+ vector<int > res (n + 1 , 0 );
22
+
23
+ int minv = 0 , maxv = 0 ;
24
+ for (int i = 0 ; i < S.size (); i ++){
25
+ int x;
26
+ if (S[i] == ' I' )
27
+ x = ++maxv;
28
+ else
29
+ x = --minv;
30
+
31
+ res[i + 1 ] = x;
32
+ }
33
+
34
+ for (int & e: res)
35
+ e += -minv;
36
+ return res;
37
+ }
38
+ };
39
+
40
+
41
+ void print_vec (const vector<int >& vec){
42
+ for (int e: vec)
43
+ cout << e << " " ;
44
+ cout << endl;
45
+ }
46
+
47
+ int main () {
48
+
49
+ string S1 = " IDID" ;
50
+ print_vec (Solution ().diStringMatch (S1));
51
+
52
+ return 0 ;
53
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/di-string-match/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-11-17
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < cassert>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Generating the result from min value 0 and max value n - 1
13
+ // /
14
+ // / Time Complexity: O(n)
15
+ // / Space Complexity: O(1)
16
+ class Solution {
17
+ public:
18
+ vector<int > diStringMatch (string S) {
19
+
20
+ int n = S.size ();
21
+ vector<int > res (n + 1 , 0 );
22
+
23
+ int l = 0 , h = n;
24
+ for (int i = 0 ; i < S.size (); i ++){
25
+ if (S[i] == ' I' )
26
+ res[i] = l ++;
27
+ else
28
+ res[i] = h --;
29
+ }
30
+
31
+ assert (l == h);
32
+ res.back () = l;
33
+ return res;
34
+ }
35
+ };
36
+
37
+
38
+ void print_vec (const vector<int >& vec){
39
+ for (int e: vec)
40
+ cout << e << " " ;
41
+ cout << endl;
42
+ }
43
+
44
+ int main () {
45
+
46
+ string S1 = " IDID" ;
47
+ print_vec (Solution ().diStringMatch (S1));
48
+
49
+ return 0 ;
50
+ }
Original file line number Diff line number Diff line change @@ -597,4 +597,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
597
597
| 939 | [ Minimum Area Rectangle] ( https://leetcode.com/problems/minimum-area-rectangle/ ) | [ solution] ( https://leetcode.com/problems/minimum-area-rectangle/solution/ ) | [ C++] ( 0939-Minimum-Area-Rectangle/cpp-0939/ ) | | |
598
598
| 940 | [ Distinct Subsequences II] ( https://leetcode.com/problems/distinct-subsequences-ii/ ) | [ solution] ( https://leetcode.com/problems/distinct-subsequences-ii/solution/ ) | [ C++] ( 0940-Distinct-Subsequences-II/cpp-0940/ ) | | |
599
599
| 941 | [ Valid Mountain Array] ( https://leetcode.com/problems/valid-mountain-array/ ) | [ solution] ( https://leetcode.com/problems/valid-mountain-array/solution/ ) | [ C++] ( 0941-Valid-Mountain-Array/cpp-0941/ ) | | |
600
+ | 942 | [ DI String Match] ( https://leetcode.com/problems/di-string-match/ ) | [ solution] ( https://leetcode.com/problems/di-string-match/solution/ ) | [ C++] ( 0942-DI-String-Match/cpp-0942/ ) | | |
600
601
| | | | | | |
You can’t perform that action at this time.
0 commit comments