Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4330e6b

Browse files
committed
feat: add solutions to lc problems: No.2574, 2575
- No.2574.Left and Right Sum Differences - No.2575.Find the Divisibility Array of a String
1 parent 5ffcc4b commit 4330e6b

File tree

8 files changed

+255
-0
lines changed

8 files changed

+255
-0
lines changed

‎solution/2500-2599/2574.Left and Right Sum Differences/README.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,61 @@ function leftRigthDifference(nums: number[]): number[] {
164164
}
165165
```
166166

167+
```ts
168+
function leftRigthDifference(nums: number[]): number[] {
169+
let left = 0;
170+
let right = nums.reduce((r, v) => r + v);
171+
return nums.map(v => {
172+
right -= v;
173+
const res = Math.abs(left - right);
174+
left += v;
175+
return res;
176+
});
177+
}
178+
```
179+
180+
### **Rust**
181+
182+
```rust
183+
impl Solution {
184+
pub fn left_rigth_difference(nums: Vec<i32>) -> Vec<i32> {
185+
let mut left = 0;
186+
let mut right = nums.iter().sum::<i32>();
187+
nums.iter()
188+
.map(|v| {
189+
right -= v;
190+
let res = (left - right).abs();
191+
left += v;
192+
res
193+
})
194+
.collect()
195+
}
196+
}
197+
```
198+
199+
### **C**
200+
201+
```c
202+
/**
203+
* Note: The returned array must be malloced, assume caller calls free().
204+
*/
205+
int *leftRigthDifference(int *nums, int numsSize, int *returnSize) {
206+
int left = 0;
207+
int right = 0;
208+
for (int i = 0; i < numsSize; i++) {
209+
right += nums[i];
210+
}
211+
int *ans = malloc(sizeof(int) * numsSize);
212+
for (int i = 0; i < numsSize; i++) {
213+
right -= nums[i];
214+
ans[i] = abs(left - right);
215+
left += nums[i];
216+
}
217+
*returnSize = numsSize;
218+
return ans;
219+
}
220+
```
221+
167222
### **...**
168223
169224
```

‎solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,61 @@ function leftRigthDifference(nums: number[]): number[] {
141141
}
142142
```
143143

144+
```ts
145+
function leftRigthDifference(nums: number[]): number[] {
146+
let left = 0;
147+
let right = nums.reduce((r, v) => r + v);
148+
return nums.map(v => {
149+
right -= v;
150+
const res = Math.abs(left - right);
151+
left += v;
152+
return res;
153+
});
154+
}
155+
```
156+
157+
### **Rust**
158+
159+
```rust
160+
impl Solution {
161+
pub fn left_rigth_difference(nums: Vec<i32>) -> Vec<i32> {
162+
let mut left = 0;
163+
let mut right = nums.iter().sum::<i32>();
164+
nums.iter()
165+
.map(|v| {
166+
right -= v;
167+
let res = (left - right).abs();
168+
left += v;
169+
res
170+
})
171+
.collect()
172+
}
173+
}
174+
```
175+
176+
### **C**
177+
178+
```c
179+
/**
180+
* Note: The returned array must be malloced, assume caller calls free().
181+
*/
182+
int *leftRigthDifference(int *nums, int numsSize, int *returnSize) {
183+
int left = 0;
184+
int right = 0;
185+
for (int i = 0; i < numsSize; i++) {
186+
right += nums[i];
187+
}
188+
int *ans = malloc(sizeof(int) * numsSize);
189+
for (int i = 0; i < numsSize; i++) {
190+
right -= nums[i];
191+
ans[i] = abs(left - right);
192+
left += nums[i];
193+
}
194+
*returnSize = numsSize;
195+
return ans;
196+
}
197+
```
198+
144199
### **...**
145200
146201
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *leftRigthDifference(int *nums, int numsSize, int *returnSize) {
5+
int left = 0;
6+
int right = 0;
7+
for (int i = 0; i < numsSize; i++) {
8+
right += nums[i];
9+
}
10+
int *ans = malloc(sizeof(int) * numsSize);
11+
for (int i = 0; i < numsSize; i++) {
12+
right -= nums[i];
13+
ans[i] = abs(left - right);
14+
left += nums[i];
15+
}
16+
*returnSize = numsSize;
17+
return ans;
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn left_rigth_difference(nums: Vec<i32>) -> Vec<i32> {
3+
let mut left = 0;
4+
let mut right = nums.iter().sum::<i32>();
5+
nums.iter()
6+
.map(|v| {
7+
right -= v;
8+
let res = (left - right).abs();
9+
left += v;
10+
res
11+
})
12+
.collect()
13+
}
14+
}

‎solution/2500-2599/2575.Find the Divisibility Array of a String/README.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,47 @@ function divisibilityArray(word: string, m: number): number[] {
142142
}
143143
```
144144

145+
### **Rust**
146+
147+
```rust
148+
impl Solution {
149+
pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {
150+
let m = m as i64;
151+
let mut x = 0i64;
152+
word.as_bytes()
153+
.iter()
154+
.map(|&c| {
155+
x = (x * 10 + i64::from(c - b'0')) % m;
156+
if x == 0 {
157+
1
158+
} else {
159+
0
160+
}
161+
})
162+
.collect()
163+
}
164+
}
165+
```
166+
167+
### **C**
168+
169+
```c
170+
/**
171+
* Note: The returned array must be malloced, assume caller calls free().
172+
*/
173+
int *divisibilityArray(char *word, int m, int *returnSize) {
174+
int n = strlen(word);
175+
int *ans = malloc(sizeof(int) * n);
176+
long long x = 0;
177+
for (int i = 0; i < n; i++) {
178+
x = (x * 10 + word[i] - '0') % m;
179+
ans[i] = x == 0 ? 1 : 0;
180+
}
181+
*returnSize = n;
182+
return ans;
183+
}
184+
```
185+
145186
### **...**
146187
147188
```

‎solution/2500-2599/2575.Find the Divisibility Array of a String/README_EN.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,47 @@ function divisibilityArray(word: string, m: number): number[] {
126126
}
127127
```
128128

129+
### **Rust**
130+
131+
```rust
132+
impl Solution {
133+
pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {
134+
let m = m as i64;
135+
let mut x = 0i64;
136+
word.as_bytes()
137+
.iter()
138+
.map(|&c| {
139+
x = (x * 10 + i64::from(c - b'0')) % m;
140+
if x == 0 {
141+
1
142+
} else {
143+
0
144+
}
145+
})
146+
.collect()
147+
}
148+
}
149+
```
150+
151+
### **C**
152+
153+
```c
154+
/**
155+
* Note: The returned array must be malloced, assume caller calls free().
156+
*/
157+
int *divisibilityArray(char *word, int m, int *returnSize) {
158+
int n = strlen(word);
159+
int *ans = malloc(sizeof(int) * n);
160+
long long x = 0;
161+
for (int i = 0; i < n; i++) {
162+
x = (x * 10 + word[i] - '0') % m;
163+
ans[i] = x == 0 ? 1 : 0;
164+
}
165+
*returnSize = n;
166+
return ans;
167+
}
168+
```
169+
129170
### **...**
130171
131172
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *divisibilityArray(char *word, int m, int *returnSize) {
5+
int n = strlen(word);
6+
int *ans = malloc(sizeof(int) * n);
7+
long long x = 0;
8+
for (int i = 0; i < n; i++) {
9+
x = (x * 10 + word[i] - '0') % m;
10+
ans[i] = x == 0 ? 1 : 0;
11+
}
12+
*returnSize = n;
13+
return ans;
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {
3+
let m = m as i64;
4+
let mut x = 0i64;
5+
word.as_bytes()
6+
.iter()
7+
.map(|&c| {
8+
x = (x * 10 + i64::from(c - b'0')) % m;
9+
if x == 0 {
10+
1
11+
} else {
12+
0
13+
}
14+
})
15+
.collect()
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /