Skip to content

Commit 99ca8b0

Browse files
committed
feat: add solutions to lc problems: No.0283, 1588, 1672
- No.0283.Move Zeroes - No.1588.Sum of All Odd Length Subarrays - No.1672.Richest Customer Wealth
1 parent 22d8974 commit 99ca8b0

File tree

15 files changed

+229
-39
lines changed

15 files changed

+229
-39
lines changed

solution/0200-0299/0283.Move Zeroes/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ var moveZeroes = function (nums) {
162162
};
163163
```
164164

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+
165185
### **Rust**
166186

167187
```rust
@@ -170,7 +190,7 @@ impl Solution {
170190
let mut i = 0;
171191
for j in 0..nums.len() {
172192
if nums[j] != 0 {
173-
if i != j {
193+
if j > i {
174194
nums[i] = nums[j];
175195
nums[j] = 0;
176196
}
@@ -181,6 +201,23 @@ impl Solution {
181201
}
182202
```
183203

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+
184221
### **...**
185222
186223
```

solution/0200-0299/0283.Move Zeroes/README_EN.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ var moveZeroes = function (nums) {
144144
};
145145
```
146146

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+
147167
### **Rust**
148168

149169
```rust
@@ -152,7 +172,7 @@ impl Solution {
152172
let mut i = 0;
153173
for j in 0..nums.len() {
154174
if nums[j] != 0 {
155-
if i != j {
175+
if j > i {
156176
nums[i] = nums[j];
157177
nums[j] = 0;
158178
}
@@ -163,6 +183,23 @@ impl Solution {
163183
}
164184
```
165185

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+
166203
### **...**
167204
168205
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

solution/0200-0299/0283.Move Zeroes/Solution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ impl Solution {
33
let mut i = 0;
44
for j in 0..nums.len() {
55
if nums[j] != 0 {
6-
if i != j {
6+
if j > i {
77
nums[i] = nums[j];
88
nums[j] = 0;
99
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

solution/1500-1599/1588.Sum of All Odd Length Subarrays/README.md

+23-8
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
157157
const n = arr.length;
158158
let res = 0;
159159
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);
164161
res += sum;
165162
for (let j = i; j < n; j++) {
166-
sum -= arr[j - i];
167-
sum += arr[j];
163+
sum += arr[j] - arr[j - i];
168164
res += sum;
169165
}
170166
}
@@ -184,8 +180,7 @@ impl Solution {
184180
let mut sum: i32 = arr[0..i].iter().sum();
185181
res += sum;
186182
for j in i..n {
187-
sum -= arr[j - i];
188-
sum += arr[j];
183+
sum += arr[j] - arr[j - i];
189184
res += sum;
190185
}
191186
i += 2;
@@ -195,6 +190,26 @@ impl Solution {
195190
}
196191
```
197192

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+
198213
### **...**
199214
200215
```

solution/1500-1599/1588.Sum of All Odd Length Subarrays/README_EN.md

+23-8
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
142142
const n = arr.length;
143143
let res = 0;
144144
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);
149146
res += sum;
150147
for (let j = i; j < n; j++) {
151-
sum -= arr[j - i];
152-
sum += arr[j];
148+
sum += arr[j] - arr[j - i];
153149
res += sum;
154150
}
155151
}
@@ -169,8 +165,7 @@ impl Solution {
169165
let mut sum: i32 = arr[0..i].iter().sum();
170166
res += sum;
171167
for j in i..n {
172-
sum -= arr[j - i];
173-
sum += arr[j];
168+
sum += arr[j] - arr[j - i];
174169
res += sum;
175170
}
176171
i += 2;
@@ -180,6 +175,26 @@ impl Solution {
180175
}
181176
```
182177

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+
183198
### **...**
184199
185200
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ impl Solution {
77
let mut sum: i32 = arr[0..i].iter().sum();
88
res += sum;
99
for j in i..n {
10-
sum -= arr[j - i];
11-
sum += arr[j];
10+
sum += arr[j] - arr[j - i];
1211
res += sum;
1312
}
1413
i += 2;

solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ function sumOddLengthSubarrays(arr: number[]): number {
22
const n = arr.length;
33
let res = 0;
44
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);
96
res += sum;
107
for (let j = i; j < n; j++) {
11-
sum -= arr[j - i];
12-
sum += arr[j];
8+
sum += arr[j] - arr[j - i];
139
res += sum;
1410
}
1511
}

solution/1600-1699/1672.Richest Customer Wealth/README.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ func maximumWealth(accounts [][]int) int {
129129
```ts
130130
function maximumWealth(accounts: number[][]): number {
131131
return accounts.reduce(
132-
(res, account) =>
132+
(r, v) =>
133133
Math.max(
134-
res,
135-
account.reduce((p, v) => p + v),
134+
r,
135+
v.reduce((r, v) => r + v),
136136
),
137137
0,
138138
);
@@ -146,13 +146,31 @@ impl Solution {
146146
pub fn maximum_wealth(accounts: Vec<Vec<i32>>) -> i32 {
147147
accounts
148148
.iter()
149-
.map(|account| account.iter().sum())
149+
.map(|v| v.iter().sum())
150150
.max()
151151
.unwrap()
152152
}
153153
}
154154
```
155155

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+
156174
### **Kotlin**
157175
158176
```kotlin

0 commit comments

Comments
 (0)