File tree Expand file tree Collapse file tree 7 files changed +298
-0
lines changed Expand file tree Collapse file tree 7 files changed +298
-0
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,117 @@ var reverse = function (x) {
120
120
};
121
121
```
122
122
123
+ ### ** C**
124
+
125
+ ``` c
126
+ int reverse (int x){
127
+ int res = 0;
128
+ while (x != 0) {
129
+ if (res > INT_MAX / 10 || res < INT_MIN / 10) {
130
+ return 0;
131
+ }
132
+ res = res * 10 + x % 10;
133
+ x /= 10;
134
+ }
135
+ return res;
136
+ }
137
+ ```
138
+
139
+ ### **Rust**
140
+
141
+ ```rust
142
+ impl Solution {
143
+ pub fn reverse(mut x: i32) -> i32 {
144
+ let is_minus = x < 0;
145
+ match x
146
+ .abs()
147
+ .to_string()
148
+ .chars()
149
+ .rev()
150
+ .collect::<String>()
151
+ .parse::<i32>()
152
+ {
153
+ Ok(x) => x * if is_minus { -1 } else { 1 },
154
+ Err(_) => 0,
155
+ }
156
+ }
157
+ }
158
+ ```
159
+
160
+ ### ** Go**
161
+
162
+ ``` go
163
+ func reverse (x int ) int {
164
+ slot := make ([]int , 11 )
165
+ count := 0
166
+ for x != 0 {
167
+ n := x%10
168
+ slot[count] = n
169
+ count++
170
+ x /= 10
171
+ }
172
+ result := 0
173
+ flag := true
174
+ for i := 0 ; i < count; i++ {
175
+ if flag && slot[i] == 0 {
176
+ continue
177
+ }
178
+ flag = false
179
+ result = 10 * result + slot[i]
180
+ }
181
+ if result > math.MaxInt32 || result < math.MinInt32 {
182
+ return 0
183
+ }
184
+ return result
185
+ }
186
+ ```
187
+
188
+ ### ** C#**
189
+
190
+ ``` rb
191
+ public class Solution {
192
+ public int Reverse (int x) {
193
+ var negative = x < 0 ;
194
+ if (negative) x = - x;
195
+ long result = 0 ;
196
+ while (x > 0 )
197
+ {
198
+ result = (result * 10 ) + x % 10 ;
199
+ x /= 10 ;
200
+ }
201
+ if (negative) result = - result;
202
+ if (result > int.MaxValue || result < int.MinValue ) result = 0 ;
203
+ return (int) result;
204
+ }
205
+ }
206
+ ```
207
+
208
+ ### ** Ruby**
209
+
210
+ ``` rb
211
+ # @ param {Integer} x
212
+ # @ return {Integer}
213
+ def reverse (x )
214
+ neg = x < 0
215
+
216
+ x = x.abs
217
+ s = ' '
218
+
219
+ x /= 10 while x > 0 && (x % 10 ).zero?
220
+
221
+ while x > 0
222
+ s += (x % 10 ).to_s
223
+ x /= 10
224
+ end
225
+
226
+ s = neg ? ' -' + s : s
227
+
228
+ # have to explicitly constraint the int boundary as per the dummy test case
229
+ res = s.to_i
230
+ res <= 214_748_364_7 && res >= - 214_748_364_8 ? res : 0
231
+ end
232
+ ```
233
+
123
234
### ** ...**
124
235
125
236
```
Original file line number Diff line number Diff line change @@ -99,6 +99,117 @@ var reverse = function (x) {
99
99
};
100
100
```
101
101
102
+ ### ** C**
103
+
104
+ ``` c
105
+ int reverse (int x){
106
+ int res = 0;
107
+ while (x != 0) {
108
+ if (res > INT_MAX / 10 || res < INT_MIN / 10) {
109
+ return 0;
110
+ }
111
+ res = res * 10 + x % 10;
112
+ x /= 10;
113
+ }
114
+ return res;
115
+ }
116
+ ```
117
+
118
+ ### **Rust**
119
+
120
+ ```rust
121
+ impl Solution {
122
+ pub fn reverse(mut x: i32) -> i32 {
123
+ let is_minus = x < 0;
124
+ match x
125
+ .abs()
126
+ .to_string()
127
+ .chars()
128
+ .rev()
129
+ .collect::<String>()
130
+ .parse::<i32>()
131
+ {
132
+ Ok(x) => x * if is_minus { -1 } else { 1 },
133
+ Err(_) => 0,
134
+ }
135
+ }
136
+ }
137
+ ```
138
+
139
+ ### ** Go**
140
+
141
+ ``` go
142
+ func reverse (x int ) int {
143
+ slot := make ([]int , 11 )
144
+ count := 0
145
+ for x != 0 {
146
+ n := x%10
147
+ slot[count] = n
148
+ count++
149
+ x /= 10
150
+ }
151
+ result := 0
152
+ flag := true
153
+ for i := 0 ; i < count; i++ {
154
+ if flag && slot[i] == 0 {
155
+ continue
156
+ }
157
+ flag = false
158
+ result = 10 * result + slot[i]
159
+ }
160
+ if result > math.MaxInt32 || result < math.MinInt32 {
161
+ return 0
162
+ }
163
+ return result
164
+ }
165
+ ```
166
+
167
+ ### ** C#**
168
+
169
+ ``` rb
170
+ public class Solution {
171
+ public int Reverse (int x) {
172
+ var negative = x < 0 ;
173
+ if (negative) x = - x;
174
+ long result = 0 ;
175
+ while (x > 0 )
176
+ {
177
+ result = (result * 10 ) + x % 10 ;
178
+ x /= 10 ;
179
+ }
180
+ if (negative) result = - result;
181
+ if (result > int.MaxValue || result < int.MinValue ) result = 0 ;
182
+ return (int) result;
183
+ }
184
+ }
185
+ ```
186
+
187
+ ### ** Ruby**
188
+
189
+ ``` rb
190
+ # @ param {Integer} x
191
+ # @ return {Integer}
192
+ def reverse (x )
193
+ neg = x < 0
194
+
195
+ x = x.abs
196
+ s = ' '
197
+
198
+ x /= 10 while x > 0 && (x % 10 ).zero?
199
+
200
+ while x > 0
201
+ s += (x % 10 ).to_s
202
+ x /= 10
203
+ end
204
+
205
+ s = neg ? ' -' + s : s
206
+
207
+ # have to explicitly constraint the int boundary as per the dummy test case
208
+ res = s.to_i
209
+ res <= 214_748_364_7 && res >= - 214_748_364_8 ? res : 0
210
+ end
211
+ ```
212
+
102
213
### ** ...**
103
214
104
215
```
Original file line number Diff line number Diff line change
1
+ int reverse (int x ){
2
+ int res = 0 ;
3
+ while (x != 0 ) {
4
+ if (res > INT_MAX / 10 || res < INT_MIN / 10 ) {
5
+ return 0 ;
6
+ }
7
+ res = res * 10 + x % 10 ;
8
+ x /= 10 ;
9
+ }
10
+ return res ;
11
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn reverse ( mut x : i32 ) -> i32 {
3
+ let is_minus = x < 0 ;
4
+ match x
5
+ . abs ( )
6
+ . to_string ( )
7
+ . chars ( )
8
+ . rev ( )
9
+ . collect :: < String > ( )
10
+ . parse :: < i32 > ( )
11
+ {
12
+ Ok ( x) => x * if is_minus { -1 } else { 1 } ,
13
+ Err ( _) => 0 ,
14
+ }
15
+ }
16
+ }
Original file line number Diff line number Diff line change @@ -236,6 +236,24 @@ public class Solution {
236
236
}
237
237
```
238
238
239
+ ### ** Rust**
240
+
241
+ ``` rust
242
+ impl Solution {
243
+ pub fn max_sub_array (nums : Vec <i32 >) -> i32 {
244
+ let n = nums . len ();
245
+ let mut res = nums [0 ];
246
+ let mut sum = nums [0 ];
247
+ for i in 1 .. n {
248
+ let num = nums [i ];
249
+ sum = num . max (sum + num );
250
+ res = res . max (sum );
251
+ }
252
+ res
253
+ }
254
+ }
255
+ ```
256
+
239
257
### ** ...**
240
258
241
259
```
Original file line number Diff line number Diff line change @@ -213,6 +213,24 @@ public class Solution {
213
213
}
214
214
```
215
215
216
+ ### ** Rust**
217
+
218
+ ``` rust
219
+ impl Solution {
220
+ pub fn max_sub_array (nums : Vec <i32 >) -> i32 {
221
+ let n = nums . len ();
222
+ let mut res = nums [0 ];
223
+ let mut sum = nums [0 ];
224
+ for i in 1 .. n {
225
+ let num = nums [i ];
226
+ sum = num . max (sum + num );
227
+ res = res . max (sum );
228
+ }
229
+ res
230
+ }
231
+ }
232
+ ```
233
+
216
234
### ** ...**
217
235
218
236
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn max_sub_array ( nums : Vec < i32 > ) -> i32 {
3
+ let n = nums. len ( ) ;
4
+ let mut res = nums[ 0 ] ;
5
+ let mut sum = nums[ 0 ] ;
6
+ for i in 1 ..n {
7
+ let num = nums[ i] ;
8
+ sum = num. max ( sum + num) ;
9
+ res = res. max ( sum) ;
10
+ }
11
+ res
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments