Skip to content

Commit a0f259a

Browse files
committed
feat: add solutions to lcp problems
* LCP 01.猜数字 * LCP 02.分式化简
1 parent 594e0b0 commit a0f259a

File tree

11 files changed

+185
-19
lines changed

11 files changed

+185
-19
lines changed

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

+26-9
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ class Solution:
5858
```java
5959
class Solution {
6060
public int game(int[] guess, int[] answer) {
61-
int times = 0;
61+
int ans = 0;
6262
for (int i = 0; i < 3; ++i) {
63-
times += (guess[i] == answer[i] ? 1 : 0);
63+
ans += guess[i] == answer[i] ? 1 : 0;
6464
}
65-
return times;
65+
return ans;
6666
}
6767
}
6868
```
@@ -73,9 +73,9 @@ class Solution {
7373
class Solution {
7474
public:
7575
int game(vector<int>& guess, vector<int>& answer) {
76-
int times = 0;
77-
for (int i = 0; i < 3; ++i) times += guess[i] == answer[i];
78-
return times;
76+
int ans = 0;
77+
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
78+
return ans;
7979
}
8080
};
8181
```
@@ -84,16 +84,33 @@ public:
8484
8585
```go
8686
func game(guess []int, answer []int) int {
87-
times := 0
87+
ans := 0
8888
for i := 0; i < 3; i++ {
8989
if guess[i] == answer[i] {
90-
times++
90+
ans++
9191
}
9292
}
93-
return times
93+
return ans
9494
}
9595
```
9696

97+
### **JavaScript**
98+
99+
```js
100+
/**
101+
* @param {number[]} guess
102+
* @param {number[]} answer
103+
* @return {number}
104+
*/
105+
var game = function (guess, answer) {
106+
let ans = 0;
107+
for (let i = 0; i < 3; ++i) {
108+
ans += guess[i] == answer[i];
109+
}
110+
return ans;
111+
};
112+
```
113+
97114
### **...**
98115

99116
```

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public:
33
int game(vector<int>& guess, vector<int>& answer) {
4-
int times = 0;
5-
for (int i = 0; i < 3; ++i) times += guess[i] == answer[i];
6-
return times;
4+
int ans = 0;
5+
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
6+
return ans;
77
}
88
};

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
func game(guess []int, answer []int) int {
2-
times := 0
2+
ans := 0
33
for i := 0; i < 3; i++ {
44
if guess[i] == answer[i] {
5-
times++
5+
ans++
66
}
77
}
8-
return times
8+
return ans
99
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public int game(int[] guess, int[] answer) {
3-
int times = 0;
3+
int ans = 0;
44
for (int i = 0; i < 3; ++i) {
5-
times += (guess[i] == answer[i] ? 1 : 0);
5+
ans += guess[i] == answer[i] ? 1 : 0;
66
}
7-
return times;
7+
return ans;
88
}
99
}

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

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

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

+76-1
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,90 @@
5252
<!-- 这里可写当前语言的特殊实现逻辑 -->
5353

5454
```python
55-
55+
class Solution:
56+
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)
5664
```
5765

5866
### **Java**
5967

6068
<!-- 这里可写当前语言的特殊实现逻辑 -->
6169

6270
```java
71+
class Solution {
72+
public int[] fraction(int[] cont) {
73+
return dfs(cont, 0);
74+
}
75+
76+
private int[] dfs(int[] cont, int i) {
77+
if (i == cont.length - 1) {
78+
return new int[]{cont[i], 1};
79+
}
80+
int[] ans = dfs(cont, i + 1);
81+
int a = ans[0], b = ans[1];
82+
return new int[]{a * cont[i] + b, a};
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
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};
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func fraction(cont []int) []int {
109+
var dfs func(i int) []int
110+
dfs = func(i int) []int {
111+
if i == len(cont)-1 {
112+
return []int{cont[i], 1}
113+
}
114+
ans := dfs(i + 1)
115+
a, b := ans[0], ans[1]
116+
return []int{a*cont[i] + b, a}
117+
}
118+
return dfs(0)
119+
}
120+
```
63121

122+
### **JavaScript**
123+
124+
```js
125+
/**
126+
* @param {number[]} cont
127+
* @return {number[]}
128+
*/
129+
var fraction = function (cont) {
130+
function dfs(i) {
131+
if (i == cont.length - 1) {
132+
return [cont[i], 1];
133+
}
134+
const [a, b] = dfs(i + 1);
135+
return [a * cont[i] + b, a];
136+
}
137+
return dfs(0);
138+
};
64139
```
65140

66141
### **...**

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
13+
};

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

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func fraction(cont []int) []int {
2+
var dfs func(i int) []int
3+
dfs = func(i int) []int {
4+
if i == len(cont)-1 {
5+
return []int{cont[i], 1}
6+
}
7+
ans := dfs(i + 1)
8+
a, b := ans[0], ans[1]
9+
return []int{a*cont[i] + b, a}
10+
}
11+
return dfs(0)
12+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int[] fraction(int[] cont) {
3+
return dfs(cont, 0);
4+
}
5+
6+
private int[] dfs(int[] cont, int i) {
7+
if (i == cont.length - 1) {
8+
return new int[]{cont[i], 1};
9+
}
10+
int[] ans = dfs(cont, i + 1);
11+
int a = ans[0], b = ans[1];
12+
return new int[]{a * cont[i] + b, a};
13+
}
14+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} cont
3+
* @return {number[]}
4+
*/
5+
var fraction = function (cont) {
6+
function dfs(i) {
7+
if (i == cont.length - 1) {
8+
return [cont[i], 1];
9+
}
10+
const [a, b] = dfs(i + 1);
11+
return [a * cont[i] + b, a];
12+
}
13+
return dfs(0);
14+
};

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

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def fraction(self, cont: List[int]) -> List[int]:
3+
def dfs(cont):
4+
if len(cont) == 1:
5+
return [cont[0], 1]
6+
a, b = dfs(cont[1:])
7+
return [a * cont[0] + b, a]
8+
9+
return dfs(cont)

0 commit comments

Comments
 (0)