Skip to content

Commit 6989d6b

Browse files
committed
feat: add solutions to lcci problem: No.08.05
No.08.05.Recursive Mulitply
1 parent 02ddd6f commit 6989d6b

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

lcci/08.05.Recursive Mulitply/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@
4949

5050
```
5151

52+
### **TypeScript**
53+
54+
```ts
55+
function multiply(A: number, B: number): number {
56+
if (A === 0 || B === 0) {
57+
return 0;
58+
}
59+
const [max, min] = [Math.max(A, B), Math.min(A, B)];
60+
return max + multiply(max, min - 1);
61+
}
62+
```
63+
64+
```ts
65+
function multiply(A: number, B: number): number {
66+
const max = Math.max(A, B);
67+
const min = Math.min(A, B);
68+
const helper = (a: number, b: number) =>
69+
(b & 1 ? a : 0) + (b > 1 ? helper(a + a, b >> 1) : 0);
70+
return helper(max, min);
71+
}
72+
```
73+
74+
### **Rust**
75+
76+
```rust
77+
impl Solution {
78+
pub fn multiply(a: i32, b: i32) -> i32 {
79+
if a == 0 || b == 0 {
80+
return 0;
81+
}
82+
a.max(b) + Self::multiply(a.max(b), a.min(b) - 1)
83+
}
84+
}
85+
```
86+
5287
### **...**
5388

5489
```

lcci/08.05.Recursive Mulitply/README_EN.md

+35
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,41 @@
4848

4949
```
5050

51+
### **TypeScript**
52+
53+
```ts
54+
function multiply(A: number, B: number): number {
55+
if (A === 0 || B === 0) {
56+
return 0;
57+
}
58+
const [max, min] = [Math.max(A, B), Math.min(A, B)];
59+
return max + multiply(max, min - 1);
60+
}
61+
```
62+
63+
```ts
64+
function multiply(A: number, B: number): number {
65+
const max = Math.max(A, B);
66+
const min = Math.min(A, B);
67+
const helper = (a: number, b: number) =>
68+
(b & 1 ? a : 0) + (b > 1 ? helper(a + a, b >> 1) : 0);
69+
return helper(max, min);
70+
}
71+
```
72+
73+
### **Rust**
74+
75+
```rust
76+
impl Solution {
77+
pub fn multiply(a: i32, b: i32) -> i32 {
78+
if a == 0 || b == 0 {
79+
return 0;
80+
}
81+
a.max(b) + Self::multiply(a.max(b), a.min(b) - 1)
82+
}
83+
}
84+
```
85+
5186
### **...**
5287

5388
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn multiply(a: i32, b: i32) -> i32 {
3+
if a == 0 || b == 0 {
4+
return 0;
5+
}
6+
a.max(b) + Self::multiply(a.max(b), a.min(b) - 1)
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function multiply(A: number, B: number): number {
2+
if (A === 0 || B === 0) {
3+
return 0;
4+
}
5+
const [max, min] = [Math.max(A, B), Math.min(A, B)];
6+
return max + multiply(max, min - 1);
7+
}

0 commit comments

Comments
 (0)