File tree 6 files changed +209
-2
lines changed
solution/0900-0999/0942.DI String Match
6 files changed +209
-2
lines changed Original file line number Diff line number Diff line change 47
47
48
48
<!-- 这里可写通用的实现逻辑 -->
49
49
50
+ 类似贪心思想,如果当前字母是 ` I ` ,我们只需要选择当前可选的最小数字,就能保证后面的数字无论怎么排列,当前数字和下一个数字一定是递增关系。` D ` 同理,选择当前可选的最大数字即可
51
+
50
52
<!-- tabs:start -->
51
53
52
54
### ** Python3**
53
55
54
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
57
56
58
``` python
57
-
59
+ class Solution :
60
+ def diStringMatch (self , s : str ) -> List[int ]:
61
+ n = len (s)
62
+ low, high = 0 , n
63
+ ans = []
64
+ for i in range (n):
65
+ if s[i] == ' I' :
66
+ ans.append(low)
67
+ low += 1
68
+ else :
69
+ ans.append(high)
70
+ high -= 1
71
+ ans.append(low)
72
+ return ans
58
73
```
59
74
60
75
### ** Java**
61
76
62
77
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
78
64
79
``` java
80
+ class Solution {
81
+ public int [] diStringMatch (String s ) {
82
+ int n = s. length();
83
+ int low = 0 , high = n;
84
+ int [] ans = new int [n + 1 ];
85
+ for (int i = 0 ; i < n; i++ ) {
86
+ if (s. charAt(i) == ' I' ) {
87
+ ans[i] = low++ ;
88
+ } else {
89
+ ans[i] = high-- ;
90
+ }
91
+ }
92
+ ans[n] = low;
93
+ return ans;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ vector<int > diStringMatch(string s) {
104
+ int n = s.size();
105
+ int low = 0, high = n;
106
+ vector<int > ans(n + 1);
107
+ for (int i = 0; i < n; ++i) {
108
+ if (s[ i] == 'I') {
109
+ ans[ i] = low++;
110
+ } else {
111
+ ans[ i] = high--;
112
+ }
113
+ }
114
+ ans[ n] = low;
115
+ return ans;
116
+ }
117
+ };
118
+ ```
65
119
120
+ ### **Go**
121
+
122
+ ```go
123
+ func diStringMatch(s string) []int {
124
+ n := len(s)
125
+ low, high := 0, n
126
+ var ans []int
127
+ for i := 0; i < n; i++ {
128
+ if s[i] == 'I' {
129
+ ans = append(ans, low)
130
+ low++
131
+ } else {
132
+ ans = append(ans, high)
133
+ high--
134
+ }
135
+ }
136
+ ans = append(ans, low)
137
+ return ans
138
+ }
66
139
```
67
140
68
141
### ** ...**
Original file line number Diff line number Diff line change 39
39
### ** Python3**
40
40
41
41
``` python
42
-
42
+ class Solution :
43
+ def diStringMatch (self , s : str ) -> List[int ]:
44
+ n = len (s)
45
+ low, high = 0 , n
46
+ ans = []
47
+ for i in range (n):
48
+ if s[i] == ' I' :
49
+ ans.append(low)
50
+ low += 1
51
+ else :
52
+ ans.append(high)
53
+ high -= 1
54
+ ans.append(low)
55
+ return ans
43
56
```
44
57
45
58
### ** Java**
46
59
47
60
``` java
61
+ class Solution {
62
+ public int [] diStringMatch (String s ) {
63
+ int n = s. length();
64
+ int low = 0 , high = n;
65
+ int [] ans = new int [n + 1 ];
66
+ for (int i = 0 ; i < n; i++ ) {
67
+ if (s. charAt(i) == ' I' ) {
68
+ ans[i] = low++ ;
69
+ } else {
70
+ ans[i] = high-- ;
71
+ }
72
+ }
73
+ ans[n] = low;
74
+ return ans;
75
+ }
76
+ }
77
+ ```
78
+
79
+ ### ** C++**
80
+
81
+ ``` cpp
82
+ class Solution {
83
+ public:
84
+ vector<int > diStringMatch(string s) {
85
+ int n = s.size();
86
+ int low = 0, high = n;
87
+ vector<int > ans(n + 1);
88
+ for (int i = 0; i < n; ++i) {
89
+ if (s[ i] == 'I') {
90
+ ans[ i] = low++;
91
+ } else {
92
+ ans[ i] = high--;
93
+ }
94
+ }
95
+ ans[ n] = low;
96
+ return ans;
97
+ }
98
+ };
99
+ ```
48
100
101
+ ### **Go**
102
+
103
+ ```go
104
+ func diStringMatch(s string) []int {
105
+ n := len(s)
106
+ low, high := 0, n
107
+ var ans []int
108
+ for i := 0; i < n; i++ {
109
+ if s[i] == 'I' {
110
+ ans = append(ans, low)
111
+ low++
112
+ } else {
113
+ ans = append(ans, high)
114
+ high--
115
+ }
116
+ }
117
+ ans = append(ans, low)
118
+ return ans
119
+ }
49
120
```
50
121
51
122
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > diStringMatch (string s) {
4
+ int n = s.size ();
5
+ int low = 0 , high = n;
6
+ vector<int > ans (n + 1 );
7
+ for (int i = 0 ; i < n; ++i) {
8
+ if (s[i] == ' I' ) {
9
+ ans[i] = low++;
10
+ } else {
11
+ ans[i] = high--;
12
+ }
13
+ }
14
+ ans[n] = low;
15
+ return ans;
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ func diStringMatch (s string ) []int {
2
+ n := len (s )
3
+ low , high := 0 , n
4
+ var ans []int
5
+ for i := 0 ; i < n ; i ++ {
6
+ if s [i ] == 'I' {
7
+ ans = append (ans , low )
8
+ low ++
9
+ } else {
10
+ ans = append (ans , high )
11
+ high --
12
+ }
13
+ }
14
+ ans = append (ans , low )
15
+ return ans
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] diStringMatch (String s ) {
3
+ int n = s .length ();
4
+ int low = 0 , high = n ;
5
+ int [] ans = new int [n + 1 ];
6
+ for (int i = 0 ; i < n ; i ++) {
7
+ if (s .charAt (i ) == 'I' ) {
8
+ ans [i ] = low ++;
9
+ } else {
10
+ ans [i ] = high --;
11
+ }
12
+ }
13
+ ans [n ] = low ;
14
+ return ans ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def diStringMatch (self , s : str ) -> List [int ]:
3
+ n = len (s )
4
+ low , high = 0 , n
5
+ ans = []
6
+ for i in range (n ):
7
+ if s [i ] == 'I' :
8
+ ans .append (low )
9
+ low += 1
10
+ else :
11
+ ans .append (high )
12
+ high -= 1
13
+ ans .append (low )
14
+ return ans
You can’t perform that action at this time.
0 commit comments