Skip to content

Commit b656625

Browse files
committed
feat: add solutions to lc problems: No.0043,1367
- No.0043.Multiply Strings - No.1367.Linked List in Binary Tree
1 parent dedd906 commit b656625

File tree

7 files changed

+400
-49
lines changed

7 files changed

+400
-49
lines changed

solution/0000-0099/0043.Multiply Strings/README.md

+17-19
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,20 @@ function addString(s1: string, s2: string): string {
158158

159159
```ts
160160
function multiply(num1: string, num2: string): string {
161-
const res = ['0'];
162161
if (num1 === '0' || num2 === '0') {
163-
return res.join('');
162+
return '0';
164163
}
165164

166165
const n = num1.length;
167166
const m = num2.length;
167+
const res = [];
168168
for (let i = 0; i < n; i++) {
169-
const num = Number(num1[n - 1 - i]);
169+
const a = Number(num1[n - i - 1]);
170170
let sum = 0;
171-
for (let j = 0; j < m || sum != 0; j++) {
172-
sum +=
173-
(Number(num2[m - 1 - j]) || 0) * num +
174-
(Number(res[i + j]) || 0);
175-
res[i + j] = `${sum % 10}`;
171+
for (let j = 0; j < m || sum !== 0; j++) {
172+
const b = Number(num2[m - j - 1] ?? 0);
173+
sum += a * b + (res[i + j] ?? 0);
174+
res[i + j] = sum % 10;
176175
sum = Math.floor(sum / 10);
177176
}
178177
}
@@ -189,29 +188,28 @@ impl Solution {
189188
if num1 == "0" || num2 == "0" {
190189
return String::from("0");
191190
}
192-
let mut res = vec![0];
193-
let num1 = num1.as_bytes();
194-
let num2 = num2.as_bytes();
195-
let n = num1.len();
196-
let m = num2.len();
191+
let (num1, num2) = (num1.as_bytes(), num2.as_bytes());
192+
let (n, m) = (num1.len(), num2.len());
193+
let mut res = vec![];
197194
for i in 0..n {
198-
let num = num1[n - i - 1] - b'0';
195+
let a = num1[n - i - 1] - b'0';
199196
let mut sum = 0;
200197
let mut j = 0;
201198
while j < m || sum != 0 {
202199
if i + j == res.len() {
203-
res.push(0);
200+
res.push(0)
204201
}
205-
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
202+
let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0';
203+
sum += a * b + res[i + j];
206204
res[i + j] = sum % 10;
207205
sum /= 10;
208206
j += 1;
209207
}
210208
}
211-
res.iter()
209+
res.into_iter()
212210
.rev()
213-
.map(|num| num.to_string())
214-
.collect::<String>()
211+
.map(|v| char::from(v + b'0'))
212+
.collect()
215213
}
216214
}
217215
```

solution/0000-0099/0043.Multiply Strings/README_EN.md

+17-19
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,20 @@ function addString(s1: string, s2: string): string {
143143

144144
```ts
145145
function multiply(num1: string, num2: string): string {
146-
const res = ['0'];
147146
if (num1 === '0' || num2 === '0') {
148-
return res.join('');
147+
return '0';
149148
}
150149

151150
const n = num1.length;
152151
const m = num2.length;
152+
const res = [];
153153
for (let i = 0; i < n; i++) {
154-
const num = Number(num1[n - 1 - i]);
154+
const a = Number(num1[n - i - 1]);
155155
let sum = 0;
156-
for (let j = 0; j < m || sum != 0; j++) {
157-
sum +=
158-
(Number(num2[m - 1 - j]) || 0) * num +
159-
(Number(res[i + j]) || 0);
160-
res[i + j] = `${sum % 10}`;
156+
for (let j = 0; j < m || sum !== 0; j++) {
157+
const b = Number(num2[m - j - 1] ?? 0);
158+
sum += a * b + (res[i + j] ?? 0);
159+
res[i + j] = sum % 10;
161160
sum = Math.floor(sum / 10);
162161
}
163162
}
@@ -174,29 +173,28 @@ impl Solution {
174173
if num1 == "0" || num2 == "0" {
175174
return String::from("0");
176175
}
177-
let mut res = vec![0];
178-
let num1 = num1.as_bytes();
179-
let num2 = num2.as_bytes();
180-
let n = num1.len();
181-
let m = num2.len();
176+
let (num1, num2) = (num1.as_bytes(), num2.as_bytes());
177+
let (n, m) = (num1.len(), num2.len());
178+
let mut res = vec![];
182179
for i in 0..n {
183-
let num = num1[n - i - 1] - b'0';
180+
let a = num1[n - i - 1] - b'0';
184181
let mut sum = 0;
185182
let mut j = 0;
186183
while j < m || sum != 0 {
187184
if i + j == res.len() {
188-
res.push(0);
185+
res.push(0)
189186
}
190-
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
187+
let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0';
188+
sum += a * b + res[i + j];
191189
res[i + j] = sum % 10;
192190
sum /= 10;
193191
j += 1;
194192
}
195193
}
196-
res.iter()
194+
res.into_iter()
197195
.rev()
198-
.map(|num| num.to_string())
199-
.collect::<String>()
196+
.map(|v| char::from(v + b'0'))
197+
.collect()
200198
}
201199
}
202200
```

solution/0000-0099/0043.Multiply Strings/Solution.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@ impl Solution {
33
if num1 == "0" || num2 == "0" {
44
return String::from("0");
55
}
6-
let mut res = vec![0];
7-
let num1 = num1.as_bytes();
8-
let num2 = num2.as_bytes();
9-
let n = num1.len();
10-
let m = num2.len();
6+
let (num1, num2) = (num1.as_bytes(), num2.as_bytes());
7+
let (n, m) = (num1.len(), num2.len());
8+
let mut res = vec![];
119
for i in 0..n {
12-
let num = num1[n - i - 1] - b'0';
10+
let a = num1[n - i - 1] - b'0';
1311
let mut sum = 0;
1412
let mut j = 0;
1513
while j < m || sum != 0 {
1614
if i + j == res.len() {
17-
res.push(0);
15+
res.push(0)
1816
}
19-
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
17+
let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0';
18+
sum += a * b + res[i + j];
2019
res[i + j] = sum % 10;
2120
sum /= 10;
2221
j += 1;
2322
}
2423
}
25-
res.iter()
24+
res.into_iter()
2625
.rev()
27-
.map(|num| num.to_string())
28-
.collect::<String>()
26+
.map(|v| char::from(v + b'0'))
27+
.collect()
2928
}
3029
}

solution/1300-1399/1367.Linked List in Binary Tree/README.md

+122
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,128 @@ class Solution {
139139
}
140140
```
141141

142+
### **TypeScript**
143+
144+
```ts
145+
/**
146+
* Definition for singly-linked list.
147+
* class ListNode {
148+
* val: number
149+
* next: ListNode | null
150+
* constructor(val?: number, next?: ListNode | null) {
151+
* this.val = (val===undefined ? 0 : val)
152+
* this.next = (next===undefined ? null : next)
153+
* }
154+
* }
155+
*/
156+
157+
/**
158+
* Definition for a binary tree node.
159+
* class TreeNode {
160+
* val: number
161+
* left: TreeNode | null
162+
* right: TreeNode | null
163+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
164+
* this.val = (val===undefined ? 0 : val)
165+
* this.left = (left===undefined ? null : left)
166+
* this.right = (right===undefined ? null : right)
167+
* }
168+
* }
169+
*/
170+
171+
const dfs = (head: ListNode | null, root: TreeNode | null) => {
172+
if (head == null) {
173+
return true;
174+
}
175+
if (root == null || head.val !== root.val) {
176+
return false;
177+
}
178+
return dfs(head.next, root.left) || dfs(head.next, root.right);
179+
};
180+
181+
function isSubPath(head: ListNode | null, root: TreeNode | null): boolean {
182+
if (root == null) {
183+
return false;
184+
}
185+
return (
186+
dfs(head, root) ||
187+
isSubPath(head, root.left) ||
188+
isSubPath(head, root.right)
189+
);
190+
}
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
// Definition for singly-linked list.
197+
// #[derive(PartialEq, Eq, Clone, Debug)]
198+
// pub struct ListNode {
199+
// pub val: i32,
200+
// pub next: Option<Box<ListNode>>
201+
// }
202+
//
203+
// impl ListNode {
204+
// #[inline]
205+
// fn new(val: i32) -> Self {
206+
// ListNode {
207+
// next: None,
208+
// val
209+
// }
210+
// }
211+
// }
212+
// Definition for a binary tree node.
213+
// #[derive(Debug, PartialEq, Eq)]
214+
// pub struct TreeNode {
215+
// pub val: i32,
216+
// pub left: Option<Rc<RefCell<TreeNode>>>,
217+
// pub right: Option<Rc<RefCell<TreeNode>>>,
218+
// }
219+
//
220+
// impl TreeNode {
221+
// #[inline]
222+
// pub fn new(val: i32) -> Self {
223+
// TreeNode {
224+
// val,
225+
// left: None,
226+
// right: None
227+
// }
228+
// }
229+
// }
230+
use std::rc::Rc;
231+
use std::cell::RefCell;
232+
impl Solution {
233+
fn dfs(head: &Option<Box<ListNode>>, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
234+
if head.is_none() {
235+
return true;
236+
}
237+
if root.is_none() {
238+
return false;
239+
}
240+
let node = head.as_ref().unwrap();
241+
let root = root.as_ref().unwrap().borrow();
242+
if node.val != root.val {
243+
return false;
244+
}
245+
Self::dfs(&node.next, &root.left) || Self::dfs(&node.next, &root.right)
246+
}
247+
248+
fn my_is_sub_path(head: &Option<Box<ListNode>>, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
249+
if root.is_none() {
250+
return false;
251+
}
252+
let node = root.as_ref().unwrap().borrow();
253+
Self::dfs(head, root)
254+
|| Self::my_is_sub_path(head, &node.left)
255+
|| Self::my_is_sub_path(head, &node.right)
256+
}
257+
258+
pub fn is_sub_path(head: Option<Box<ListNode>>, root: Option<Rc<RefCell<TreeNode>>>) -> bool {
259+
Self::my_is_sub_path(&head, &root)
260+
}
261+
}
262+
```
263+
142264
### **...**
143265

144266
```

0 commit comments

Comments
 (0)