Skip to content

Commit fca57df

Browse files
authored
feat: add solutions to lcp problems: No.01~03 (doocs#1456)
* No.01.猜数字 * No.02.分式化简 * No.03.机器人大冒险
1 parent a2d9d8d commit fca57df

File tree

23 files changed

+579
-117
lines changed

23 files changed

+579
-117
lines changed

lcp/LCP 01. 猜数字/README.md

+34-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:遍历**
43+
44+
我们同时遍历两个数组,如果对应位置的元素相等,那么答案加一。
45+
46+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度,本题中 $n=3$。空间复杂度 $O(1)$。
47+
4248
<!-- tabs:start -->
4349

4450
### **Python3**
@@ -48,7 +54,7 @@
4854
```python
4955
class Solution:
5056
def game(self, guess: List[int], answer: List[int]) -> int:
51-
return sum(1 for i in range(3) if guess[i] == answer[i])
57+
return sum(a == b for a, b in zip(guess, answer))
5258
```
5359

5460
### **Java**
@@ -60,7 +66,9 @@ class Solution {
6066
public int game(int[] guess, int[] answer) {
6167
int ans = 0;
6268
for (int i = 0; i < 3; ++i) {
63-
ans += guess[i] == answer[i] ? 1 : 0;
69+
if (guess[i] == answer[i]) {
70+
++ans;
71+
}
6472
}
6573
return ans;
6674
}
@@ -74,7 +82,9 @@ class Solution {
7482
public:
7583
int game(vector<int>& guess, vector<int>& answer) {
7684
int ans = 0;
77-
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
85+
for (int i = 0; i < 3; ++i) {
86+
ans += guess[i] == answer[i];
87+
}
7888
return ans;
7989
}
8090
};
@@ -83,14 +93,27 @@ public:
8393
### **Go**
8494
8595
```go
86-
func game(guess []int, answer []int) int {
87-
ans := 0
88-
for i := 0; i < 3; i++ {
89-
if guess[i] == answer[i] {
96+
func game(guess []int, answer []int) (ans int) {
97+
for i, a := range guess {
98+
if a == answer[i] {
9099
ans++
91100
}
92101
}
93-
return ans
102+
return
103+
}
104+
```
105+
106+
### **TypeScript**
107+
108+
```ts
109+
function game(guess: number[], answer: number[]): number {
110+
let ans = 0;
111+
for (let i = 0; i < 3; ++i) {
112+
if (guess[i] === answer[i]) {
113+
++ans;
114+
}
115+
}
116+
return ans;
94117
}
95118
```
96119

@@ -105,7 +128,9 @@ func game(guess []int, answer []int) int {
105128
var game = function (guess, answer) {
106129
let ans = 0;
107130
for (let i = 0; i < 3; ++i) {
108-
ans += guess[i] === answer[i];
131+
if (guess[i] === answer[i]) {
132+
++ans;
133+
}
109134
}
110135
return ans;
111136
};

lcp/LCP 01. 猜数字/Solution.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class Solution {
2-
public:
3-
int game(vector<int>& guess, vector<int>& answer) {
4-
int ans = 0;
5-
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
6-
return ans;
7-
}
1+
class Solution {
2+
public:
3+
int game(vector<int>& guess, vector<int>& answer) {
4+
int ans = 0;
5+
for (int i = 0; i < 3; ++i) {
6+
ans += guess[i] == answer[i];
7+
}
8+
return ans;
9+
}
810
};

lcp/LCP 01. 猜数字/Solution.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
func game(guess []int, answer []int) int {
2-
ans := 0
3-
for i := 0; i < 3; i++ {
4-
if guess[i] == answer[i] {
1+
func game(guess []int, answer []int) (ans int) {
2+
for i, a := range guess {
3+
if a == answer[i] {
54
ans++
65
}
76
}
8-
return ans
7+
return
98
}

lcp/LCP 01. 猜数字/Solution.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
class Solution {
2-
public int game(int[] guess, int[] answer) {
3-
int ans = 0;
4-
for (int i = 0; i < 3; ++i) {
5-
ans += guess[i] == answer[i] ? 1 : 0;
6-
}
7-
return ans;
8-
}
1+
class Solution {
2+
public int game(int[] guess, int[] answer) {
3+
int ans = 0;
4+
for (int i = 0; i < 3; ++i) {
5+
if (guess[i] == answer[i]) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}
911
}

lcp/LCP 01. 猜数字/Solution.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
var game = function (guess, answer) {
77
let ans = 0;
88
for (let i = 0; i < 3; ++i) {
9-
ans += guess[i] === answer[i];
9+
if (guess[i] === answer[i]) {
10+
++ans;
11+
}
1012
}
1113
return ans;
1214
};

lcp/LCP 01. 猜数字/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class Solution:
2-
def game(self, guess: List[int], answer: List[int]) -> int:
3-
return sum(1 for i in range(3) if guess[i] == answer[i])
1+
class Solution:
2+
def game(self, guess: List[int], answer: List[int]) -> int:
3+
return sum(a == b for a, b in zip(guess, answer))

lcp/LCP 01. 猜数字/Solution.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function game(guess: number[], answer: number[]): number {
2+
let ans = 0;
3+
for (let i = 0; i < 3; ++i) {
4+
if (guess[i] === answer[i]) {
5+
++ans;
6+
}
7+
}
8+
return ans;
9+
}

lcp/LCP 02. 分式化简/README.md

+75-23
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:DFS + 数学**
49+
50+
我们设计一个函数 $dfs(i)$,表示从下标 $i$ 开始到最后一个元素的连分数的值,那么答案就是 $dfs(0)$。
51+
52+
函数 $dfs(i)$ 的执行逻辑如下:
53+
54+
如果 $i = n - 1$,只有一个元素,那么它的值就是 $cont[i]$,分母为 $1$,返回 $[cont[i], 1]$。
55+
56+
否则,我们递归调用 $dfs(i + 1)$,记返回值为 $[a, b]$,那么 $dfs(i)= 1 + \frac{1}{\frac{a}{b}}$,即 $dfs(i) = \frac{a \times cont[i] + b}{a}$,分子为 $x = a \times cont[i] + b$,分母为 $y = a$,我们求出 $x$ 和 $y$ 的最大公约数 $g$,最终返回 $[\frac{x}{g}, \frac{y}{g}]$。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $cont$ 的长度。
59+
4860
<!-- tabs:start -->
4961

5062
### **Python3**
@@ -54,13 +66,15 @@
5466
```python
5567
class Solution:
5668
def fraction(self, cont: List[int]) -> List[int]:
57-
def dfs(cont):
58-
if len(cont) == 1:
59-
return [cont[0], 1]
60-
a, b = dfs(cont[1:])
61-
return [a * cont[0] + b, a]
62-
63-
return dfs(cont)
69+
def dfs(i: int) -> List[int]:
70+
if i == len(cont) - 1:
71+
return [cont[i], 1]
72+
a, b = dfs(i + 1)
73+
x, y = a * cont[i] + b, a
74+
g = gcd(x, y)
75+
return [x // g, y // g]
76+
77+
return dfs(0)
6478
```
6579

6680
### **Java**
@@ -69,17 +83,26 @@ class Solution:
6983

7084
```java
7185
class Solution {
86+
private int[] cont;
87+
7288
public int[] fraction(int[] cont) {
73-
return dfs(cont, 0);
89+
this.cont = cont;
90+
return dfs(0);
7491
}
7592

76-
private int[] dfs(int[] cont, int i) {
93+
private int[] dfs(int i) {
7794
if (i == cont.length - 1) {
7895
return new int[] {cont[i], 1};
7996
}
80-
int[] ans = dfs(cont, i + 1);
81-
int a = ans[0], b = ans[1];
82-
return new int[] {a * cont[i] + b, a};
97+
int[] next = dfs(i + 1);
98+
int a = next[0], b = next[1];
99+
int x = a * cont[i] + b, y = a;
100+
int g = gcd(x, y);
101+
return new int[] {x / g, y / g};
102+
}
103+
104+
private int gcd(int a, int b) {
105+
return b == 0 ? a : gcd(b, a % b);
83106
}
84107
}
85108
```
@@ -90,14 +113,18 @@ class Solution {
90113
class Solution {
91114
public:
92115
vector<int> fraction(vector<int>& cont) {
93-
return dfs(cont, 0);
94-
}
95-
96-
vector<int> dfs(vector<int>& cont, int i) {
97-
if (i == cont.size() - 1) return {cont[i], 1};
98-
vector<int> ans = dfs(cont, i + 1);
99-
int a = ans[0], b = ans[1];
100-
return {a * cont[i] + b, a};
116+
function<vector<int>(int)> dfs = [&](int i) {
117+
if (i == cont.size() - 1) {
118+
return vector<int>{cont[i], 1};
119+
}
120+
vector<int> next = dfs(i + 1);
121+
int a = next[0], b = next[1];
122+
int x = a * cont[i] + b;
123+
int y = a;
124+
int g = __gcd(x, y);
125+
return vector<int>{x / g, y / g};
126+
};
127+
return dfs(0);
101128
}
102129
};
103130
```
@@ -119,6 +146,26 @@ func fraction(cont []int) []int {
119146
}
120147
```
121148

149+
### **TypeScript**
150+
151+
```ts
152+
function fraction(cont: number[]): number[] {
153+
const dfs = (i: number): number[] => {
154+
if (i === cont.length - 1) {
155+
return [cont[i], 1];
156+
}
157+
const [a, b] = dfs(i + 1);
158+
const [x, y] = [a * cont[i] + b, a];
159+
const g = gcd(x, y);
160+
return [x / g, y / g];
161+
};
162+
const gcd = (a: number, b: number): number => {
163+
return b === 0 ? a : gcd(b, a % b);
164+
};
165+
return dfs(0);
166+
}
167+
```
168+
122169
### **JavaScript**
123170

124171
```js
@@ -127,13 +174,18 @@ func fraction(cont []int) []int {
127174
* @return {number[]}
128175
*/
129176
var fraction = function (cont) {
130-
function dfs(i) {
177+
const dfs = i => {
131178
if (i === cont.length - 1) {
132179
return [cont[i], 1];
133180
}
134181
const [a, b] = dfs(i + 1);
135-
return [a * cont[i] + b, a];
136-
}
182+
const [x, y] = [a * cont[i] + b, a];
183+
const g = gcd(x, y);
184+
return [Math.floor(x / g), Math.floor(y / g)];
185+
};
186+
const gcd = (a, b) => {
187+
return b === 0 ? a : gcd(b, a % b);
188+
};
137189
return dfs(0);
138190
};
139191
```

lcp/LCP 02. 分式化简/Solution.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
class Solution {
2-
public:
3-
vector<int> fraction(vector<int>& cont) {
4-
return dfs(cont, 0);
5-
}
6-
7-
vector<int> dfs(vector<int>& cont, int i) {
8-
if (i == cont.size() - 1) return {cont[i], 1};
9-
vector<int> ans = dfs(cont, i + 1);
10-
int a = ans[0], b = ans[1];
11-
return {a * cont[i] + b, a};
12-
}
1+
class Solution {
2+
public:
3+
vector<int> fraction(vector<int>& cont) {
4+
function<vector<int>(int)> dfs = [&](int i) {
5+
if (i == cont.size() - 1) {
6+
return vector<int>{cont[i], 1};
7+
}
8+
vector<int> next = dfs(i + 1);
9+
int a = next[0], b = next[1];
10+
int x = a * cont[i] + b;
11+
int y = a;
12+
int g = __gcd(x, y);
13+
return vector<int>{x / g, y / g};
14+
};
15+
return dfs(0);
16+
}
1317
};

lcp/LCP 02. 分式化简/Solution.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
func fraction(cont []int) []int {
2-
var dfs func(i int) []int
2+
var dfs func(int) []int
33
dfs = func(i int) []int {
44
if i == len(cont)-1 {
55
return []int{cont[i], 1}
66
}
7-
ans := dfs(i + 1)
8-
a, b := ans[0], ans[1]
9-
return []int{a*cont[i] + b, a}
7+
next := dfs(i + 1)
8+
a, b := next[0], next[1]
9+
x, y := a*cont[i]+b, a
10+
g := gcd(x, y)
11+
return []int{x / g, y / g}
1012
}
1113
return dfs(0)
14+
}
15+
16+
func gcd(a, b int) int {
17+
if b == 0 {
18+
return a
19+
}
20+
return gcd(b, a%b)
1221
}

0 commit comments

Comments
 (0)