File tree 15 files changed +229
-39
lines changed
0200-0299/0283.Move Zeroes
1500-1599/1588.Sum of All Odd Length Subarrays
1600-1699/1672.Richest Customer Wealth
15 files changed +229
-39
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,26 @@ var moveZeroes = function (nums) {
162
162
};
163
163
```
164
164
165
+ ### ** TypeScript**
166
+
167
+ ``` ts
168
+ /**
169
+ Do not return anything, modify nums in-place instead.
170
+ */
171
+ function moveZeroes(nums : number []): void {
172
+ const n = nums .length ;
173
+ let i = 0 ;
174
+ for (let j = 0 ; j < n ; j ++ ) {
175
+ if (nums [j ]) {
176
+ if (j > i ) {
177
+ [nums [i ], nums [j ]] = [nums [j ], 0 ];
178
+ }
179
+ i ++ ;
180
+ }
181
+ }
182
+ }
183
+ ```
184
+
165
185
### ** Rust**
166
186
167
187
``` rust
@@ -170,7 +190,7 @@ impl Solution {
170
190
let mut i = 0 ;
171
191
for j in 0 .. nums . len () {
172
192
if nums [j ] != 0 {
173
- if i != j {
193
+ if j > i {
174
194
nums [i ] = nums [j ];
175
195
nums [j ] = 0 ;
176
196
}
@@ -181,6 +201,23 @@ impl Solution {
181
201
}
182
202
```
183
203
204
+ ### ** C**
205
+
206
+ ``` c
207
+ void moveZeroes (int * nums, int numsSize) {
208
+ int i = 0;
209
+ for (int j = 0; j < numsSize; j++) {
210
+ if (nums[ j] != 0) {
211
+ if (j > i) {
212
+ nums[ i] = nums[ j] ;
213
+ nums[ j] = 0;
214
+ }
215
+ i++;
216
+ }
217
+ }
218
+ }
219
+ ```
220
+
184
221
### **...**
185
222
186
223
```
Original file line number Diff line number Diff line change @@ -144,6 +144,26 @@ var moveZeroes = function (nums) {
144
144
};
145
145
```
146
146
147
+ ### ** TypeScript**
148
+
149
+ ``` ts
150
+ /**
151
+ Do not return anything, modify nums in-place instead.
152
+ */
153
+ function moveZeroes(nums : number []): void {
154
+ const n = nums .length ;
155
+ let i = 0 ;
156
+ for (let j = 0 ; j < n ; j ++ ) {
157
+ if (nums [j ]) {
158
+ if (j > i ) {
159
+ [nums [i ], nums [j ]] = [nums [j ], 0 ];
160
+ }
161
+ i ++ ;
162
+ }
163
+ }
164
+ }
165
+ ```
166
+
147
167
### ** Rust**
148
168
149
169
``` rust
@@ -152,7 +172,7 @@ impl Solution {
152
172
let mut i = 0 ;
153
173
for j in 0 .. nums . len () {
154
174
if nums [j ] != 0 {
155
- if i != j {
175
+ if j > i {
156
176
nums [i ] = nums [j ];
157
177
nums [j ] = 0 ;
158
178
}
@@ -163,6 +183,23 @@ impl Solution {
163
183
}
164
184
```
165
185
186
+ ### ** C**
187
+
188
+ ``` c
189
+ void moveZeroes (int * nums, int numsSize) {
190
+ int i = 0;
191
+ for (int j = 0; j < numsSize; j++) {
192
+ if (nums[ j] != 0) {
193
+ if (j > i) {
194
+ nums[ i] = nums[ j] ;
195
+ nums[ j] = 0;
196
+ }
197
+ i++;
198
+ }
199
+ }
200
+ }
201
+ ```
202
+
166
203
### **...**
167
204
168
205
```
Original file line number Diff line number Diff line change
1
+ void moveZeroes (int * nums , int numsSize ) {
2
+ int i = 0 ;
3
+ for (int j = 0 ; j < numsSize ; j ++ ) {
4
+ if (nums [j ] != 0 ) {
5
+ if (j > i ) {
6
+ nums [i ] = nums [j ];
7
+ nums [j ] = 0 ;
8
+ }
9
+ i ++ ;
10
+ }
11
+ }
12
+ }
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ impl Solution {
3
3
let mut i = 0 ;
4
4
for j in 0 ..nums. len ( ) {
5
5
if nums[ j] != 0 {
6
- if i != j {
6
+ if j > i {
7
7
nums[ i] = nums[ j] ;
8
8
nums[ j] = 0 ;
9
9
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ Do not return anything, modify nums in-place instead.
3
+ */
4
+ function moveZeroes ( nums : number [ ] ) : void {
5
+ const n = nums . length ;
6
+ let i = 0 ;
7
+ for ( let j = 0 ; j < n ; j ++ ) {
8
+ if ( nums [ j ] ) {
9
+ if ( j > i ) {
10
+ [ nums [ i ] , nums [ j ] ] = [ nums [ j ] , 0 ] ;
11
+ }
12
+ i ++ ;
13
+ }
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -157,14 +157,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
157
157
const n = arr .length ;
158
158
let res = 0 ;
159
159
for (let i = 1 ; i <= n ; i += 2 ) {
160
- let sum = 0 ;
161
- for (let j = 0 ; j < i ; j ++ ) {
162
- sum += arr [j ];
163
- }
160
+ let sum = arr .slice (0 , i ).reduce ((r , v ) => r + v );
164
161
res += sum ;
165
162
for (let j = i ; j < n ; j ++ ) {
166
- sum -= arr [j - i ];
167
- sum += arr [j ];
163
+ sum += arr [j ] - arr [j - i ];
168
164
res += sum ;
169
165
}
170
166
}
@@ -184,8 +180,7 @@ impl Solution {
184
180
let mut sum : i32 = arr [0 .. i ]. iter (). sum ();
185
181
res += sum ;
186
182
for j in i .. n {
187
- sum -= arr [j - i ];
188
- sum += arr [j ];
183
+ sum += arr [j ] - arr [j - i ];
189
184
res += sum ;
190
185
}
191
186
i += 2 ;
@@ -195,6 +190,26 @@ impl Solution {
195
190
}
196
191
```
197
192
193
+ ### ** C**
194
+
195
+ ``` c
196
+ int sumOddLengthSubarrays (int * arr, int arrSize) {
197
+ int ans = 0;
198
+ for (int i = 1; i <= arrSize; i += 2) {
199
+ int sum = 0;
200
+ for (int j = 0; j < i; j++) {
201
+ sum += arr[ j] ;
202
+ }
203
+ ans += sum;
204
+ for (int j = i; j < arrSize; j++) {
205
+ sum += arr[ j] - arr[ j - i] ;
206
+ ans += sum;
207
+ }
208
+ }
209
+ return ans;
210
+ }
211
+ ```
212
+
198
213
### **...**
199
214
200
215
```
Original file line number Diff line number Diff line change @@ -142,14 +142,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
142
142
const n = arr .length ;
143
143
let res = 0 ;
144
144
for (let i = 1 ; i <= n ; i += 2 ) {
145
- let sum = 0 ;
146
- for (let j = 0 ; j < i ; j ++ ) {
147
- sum += arr [j ];
148
- }
145
+ let sum = arr .slice (0 , i ).reduce ((r , v ) => r + v );
149
146
res += sum ;
150
147
for (let j = i ; j < n ; j ++ ) {
151
- sum -= arr [j - i ];
152
- sum += arr [j ];
148
+ sum += arr [j ] - arr [j - i ];
153
149
res += sum ;
154
150
}
155
151
}
@@ -169,8 +165,7 @@ impl Solution {
169
165
let mut sum : i32 = arr [0 .. i ]. iter (). sum ();
170
166
res += sum ;
171
167
for j in i .. n {
172
- sum -= arr [j - i ];
173
- sum += arr [j ];
168
+ sum += arr [j ] - arr [j - i ];
174
169
res += sum ;
175
170
}
176
171
i += 2 ;
@@ -180,6 +175,26 @@ impl Solution {
180
175
}
181
176
```
182
177
178
+ ### ** C**
179
+
180
+ ``` c
181
+ int sumOddLengthSubarrays (int * arr, int arrSize) {
182
+ int ans = 0;
183
+ for (int i = 1; i <= arrSize; i += 2) {
184
+ int sum = 0;
185
+ for (int j = 0; j < i; j++) {
186
+ sum += arr[ j] ;
187
+ }
188
+ ans += sum;
189
+ for (int j = i; j < arrSize; j++) {
190
+ sum += arr[ j] - arr[ j - i] ;
191
+ ans += sum;
192
+ }
193
+ }
194
+ return ans;
195
+ }
196
+ ```
197
+
183
198
### **...**
184
199
185
200
```
Original file line number Diff line number Diff line change
1
+ int sumOddLengthSubarrays (int * arr , int arrSize ) {
2
+ int ans = 0 ;
3
+ for (int i = 1 ; i <= arrSize ; i += 2 ) {
4
+ int sum = 0 ;
5
+ for (int j = 0 ; j < i ; j ++ ) {
6
+ sum += arr [j ];
7
+ }
8
+ ans += sum ;
9
+ for (int j = i ; j < arrSize ; j ++ ) {
10
+ sum += arr [j ] - arr [j - i ];
11
+ ans += sum ;
12
+ }
13
+ }
14
+ return ans ;
15
+ }
Original file line number Diff line number Diff line change @@ -7,8 +7,7 @@ impl Solution {
7
7
let mut sum: i32 = arr[ 0 ..i] . iter ( ) . sum ( ) ;
8
8
res += sum;
9
9
for j in i..n {
10
- sum -= arr[ j - i] ;
11
- sum += arr[ j] ;
10
+ sum += arr[ j] - arr[ j - i] ;
12
11
res += sum;
13
12
}
14
13
i += 2 ;
Original file line number Diff line number Diff line change @@ -2,14 +2,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
2
2
const n = arr . length ;
3
3
let res = 0 ;
4
4
for ( let i = 1 ; i <= n ; i += 2 ) {
5
- let sum = 0 ;
6
- for ( let j = 0 ; j < i ; j ++ ) {
7
- sum += arr [ j ] ;
8
- }
5
+ let sum = arr . slice ( 0 , i ) . reduce ( ( r , v ) => r + v ) ;
9
6
res += sum ;
10
7
for ( let j = i ; j < n ; j ++ ) {
11
- sum -= arr [ j - i ] ;
12
- sum += arr [ j ] ;
8
+ sum += arr [ j ] - arr [ j - i ] ;
13
9
res += sum ;
14
10
}
15
11
}
Original file line number Diff line number Diff line change @@ -129,10 +129,10 @@ func maximumWealth(accounts [][]int) int {
129
129
``` ts
130
130
function maximumWealth(accounts : number [][]): number {
131
131
return accounts .reduce (
132
- (res , account ) =>
132
+ (r , v ) =>
133
133
Math .max (
134
- res ,
135
- account .reduce ((p , v ) => p + v ),
134
+ r ,
135
+ v .reduce ((r , v ) => r + v ),
136
136
),
137
137
0 ,
138
138
);
@@ -146,13 +146,31 @@ impl Solution {
146
146
pub fn maximum_wealth (accounts : Vec <Vec <i32 >>) -> i32 {
147
147
accounts
148
148
. iter ()
149
- . map (| account | account . iter (). sum ())
149
+ . map (| v | v . iter (). sum ())
150
150
. max ()
151
151
. unwrap ()
152
152
}
153
153
}
154
154
```
155
155
156
+ ### ** C**
157
+
158
+ ``` c
159
+ #define max (a, b ) (((a) > (b)) ? (a) : (b))
160
+
161
+ int maximumWealth (int ** accounts, int accountsSize, int * accountsColSize) {
162
+ int ans = INT_MIN;
163
+ for (int i = 0; i < accountsSize; i++) {
164
+ int sum = 0;
165
+ for (int j = 0; j < accountsColSize[ i] ; j++) {
166
+ sum += accounts[ i] [ j ] ;
167
+ }
168
+ ans = max(ans, sum);
169
+ }
170
+ return ans;
171
+ }
172
+ ```
173
+
156
174
### **Kotlin**
157
175
158
176
```kotlin
You can’t perform that action at this time.
0 commit comments