Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lcp problems: No.01~03 #1456

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions lcp/LCP 01. 猜数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@

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

**方法一:遍历**

我们同时遍历两个数组,如果对应位置的元素相等,那么答案加一。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度,本题中 $n=3$。空间复杂度 $O(1)$。

<!-- tabs:start -->

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

### **Java**
Expand All @@ -60,7 +66,9 @@ class Solution {
public int game(int[] guess, int[] answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) {
ans += guess[i] == answer[i] ? 1 : 0;
if (guess[i] == answer[i]) {
++ans;
}
}
return ans;
}
Expand All @@ -74,7 +82,9 @@ class Solution {
public:
int game(vector<int>& guess, vector<int>& answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
for (int i = 0; i < 3; ++i) {
ans += guess[i] == answer[i];
}
return ans;
}
};
Expand All @@ -83,14 +93,27 @@ public:
### **Go**

```go
func game(guess []int, answer []int) int {
ans := 0
for i := 0; i < 3; i++ {
if guess[i] == answer[i] {
func game(guess []int, answer []int) (ans int) {
for i, a := range guess {
if a == answer[i] {
ans++
}
}
return ans
return
}
```

### **TypeScript**

```ts
function game(guess: number[], answer: number[]): number {
let ans = 0;
for (let i = 0; i < 3; ++i) {
if (guess[i] === answer[i]) {
++ans;
}
}
return ans;
}
```

Expand All @@ -105,7 +128,9 @@ func game(guess []int, answer []int) int {
var game = function (guess, answer) {
let ans = 0;
for (let i = 0; i < 3; ++i) {
ans += guess[i] === answer[i];
if (guess[i] === answer[i]) {
++ans;
}
}
return ans;
};
Expand Down
16 changes: 9 additions & 7 deletions lcp/LCP 01. 猜数字/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class Solution {
public:
int game(vector<int>& guess, vector<int>& answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i];
return ans;
}
class Solution {
public:
int game(vector<int>& guess, vector<int>& answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) {
ans += guess[i] == answer[i];
}
return ans;
}
};
9 changes: 4 additions & 5 deletions lcp/LCP 01. 猜数字/Solution.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
func game(guess []int, answer []int) int {
ans := 0
for i := 0; i < 3; i++ {
if guess[i] == answer[i] {
func game(guess []int, answer []int) (ans int) {
for i, a := range guess {
if a == answer[i] {
ans++
}
}
return ans
return
}
18 changes: 10 additions & 8 deletions lcp/LCP 01. 猜数字/Solution.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Solution {
public int game(int[] guess, int[] answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) {
ans += guess[i] == answer[i] ? 1 : 0;
}
return ans;
}
class Solution {
public int game(int[] guess, int[] answer) {
int ans = 0;
for (int i = 0; i < 3; ++i) {
if (guess[i] == answer[i]) {
++ans;
}
}
return ans;
}
}
4 changes: 3 additions & 1 deletion lcp/LCP 01. 猜数字/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
var game = function (guess, answer) {
let ans = 0;
for (let i = 0; i < 3; ++i) {
ans += guess[i] === answer[i];
if (guess[i] === answer[i]) {
++ans;
}
}
return ans;
};
6 changes: 3 additions & 3 deletions lcp/LCP 01. 猜数字/Solution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def game(self, guess: List[int], answer: List[int]) -> int:
return sum(1 for i in range(3) if guess[i] == answer[i])
class Solution:
def game(self, guess: List[int], answer: List[int]) -> int:
return sum(a == b for a, b in zip(guess, answer))
9 changes: 9 additions & 0 deletions lcp/LCP 01. 猜数字/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function game(guess: number[], answer: number[]): number {
let ans = 0;
for (let i = 0; i < 3; ++i) {
if (guess[i] === answer[i]) {
++ans;
}
}
return ans;
}
98 changes: 75 additions & 23 deletions lcp/LCP 02. 分式化简/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@

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

**方法一:DFS + 数学**

我们设计一个函数 $dfs(i)$,表示从下标 $i$ 开始到最后一个元素的连分数的值,那么答案就是 $dfs(0)$。

函数 $dfs(i)$ 的执行逻辑如下:

如果 $i = n - 1$,只有一个元素,那么它的值就是 $cont[i]$,分母为 $1$,返回 $[cont[i], 1]$。

否则,我们递归调用 $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}]$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $cont$ 的长度。

<!-- tabs:start -->

### **Python3**
Expand All @@ -54,13 +66,15 @@
```python
class Solution:
def fraction(self, cont: List[int]) -> List[int]:
def dfs(cont):
if len(cont) == 1:
return [cont[0], 1]
a, b = dfs(cont[1:])
return [a * cont[0] + b, a]

return dfs(cont)
def dfs(i: int) -> List[int]:
if i == len(cont) - 1:
return [cont[i], 1]
a, b = dfs(i + 1)
x, y = a * cont[i] + b, a
g = gcd(x, y)
return [x // g, y // g]

return dfs(0)
```

### **Java**
Expand All @@ -69,17 +83,26 @@ class Solution:

```java
class Solution {
private int[] cont;

public int[] fraction(int[] cont) {
return dfs(cont, 0);
this.cont = cont;
return dfs(0);
}

private int[] dfs(int[] cont, int i) {
private int[] dfs(int i) {
if (i == cont.length - 1) {
return new int[] {cont[i], 1};
}
int[] ans = dfs(cont, i + 1);
int a = ans[0], b = ans[1];
return new int[] {a * cont[i] + b, a};
int[] next = dfs(i + 1);
int a = next[0], b = next[1];
int x = a * cont[i] + b, y = a;
int g = gcd(x, y);
return new int[] {x / g, y / g};
}

private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
```
Expand All @@ -90,14 +113,18 @@ class Solution {
class Solution {
public:
vector<int> fraction(vector<int>& cont) {
return dfs(cont, 0);
}

vector<int> dfs(vector<int>& cont, int i) {
if (i == cont.size() - 1) return {cont[i], 1};
vector<int> ans = dfs(cont, i + 1);
int a = ans[0], b = ans[1];
return {a * cont[i] + b, a};
function<vector<int>(int)> dfs = [&](int i) {
if (i == cont.size() - 1) {
return vector<int>{cont[i], 1};
}
vector<int> next = dfs(i + 1);
int a = next[0], b = next[1];
int x = a * cont[i] + b;
int y = a;
int g = __gcd(x, y);
return vector<int>{x / g, y / g};
};
return dfs(0);
}
};
```
Expand All @@ -119,6 +146,26 @@ func fraction(cont []int) []int {
}
```

### **TypeScript**

```ts
function fraction(cont: number[]): number[] {
const dfs = (i: number): number[] => {
if (i === cont.length - 1) {
return [cont[i], 1];
}
const [a, b] = dfs(i + 1);
const [x, y] = [a * cont[i] + b, a];
const g = gcd(x, y);
return [x / g, y / g];
};
const gcd = (a: number, b: number): number => {
return b === 0 ? a : gcd(b, a % b);
};
return dfs(0);
}
```

### **JavaScript**

```js
Expand All @@ -127,13 +174,18 @@ func fraction(cont []int) []int {
* @return {number[]}
*/
var fraction = function (cont) {
function dfs(i) {
const dfs = i => {
if (i === cont.length - 1) {
return [cont[i], 1];
}
const [a, b] = dfs(i + 1);
return [a * cont[i] + b, a];
}
const [x, y] = [a * cont[i] + b, a];
const g = gcd(x, y);
return [Math.floor(x / g), Math.floor(y / g)];
};
const gcd = (a, b) => {
return b === 0 ? a : gcd(b, a % b);
};
return dfs(0);
};
```
Expand Down
28 changes: 16 additions & 12 deletions lcp/LCP 02. 分式化简/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
class Solution {
public:
vector<int> fraction(vector<int>& cont) {
return dfs(cont, 0);
}

vector<int> dfs(vector<int>& cont, int i) {
if (i == cont.size() - 1) return {cont[i], 1};
vector<int> ans = dfs(cont, i + 1);
int a = ans[0], b = ans[1];
return {a * cont[i] + b, a};
}
class Solution {
public:
vector<int> fraction(vector<int>& cont) {
function<vector<int>(int)> dfs = [&](int i) {
if (i == cont.size() - 1) {
return vector<int>{cont[i], 1};
}
vector<int> next = dfs(i + 1);
int a = next[0], b = next[1];
int x = a * cont[i] + b;
int y = a;
int g = __gcd(x, y);
return vector<int>{x / g, y / g};
};
return dfs(0);
}
};
17 changes: 13 additions & 4 deletions lcp/LCP 02. 分式化简/Solution.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
func fraction(cont []int) []int {
var dfs func(i int) []int
var dfs func(int) []int
dfs = func(i int) []int {
if i == len(cont)-1 {
return []int{cont[i], 1}
}
ans := dfs(i + 1)
a, b := ans[0], ans[1]
return []int{a*cont[i] + b, a}
next := dfs(i + 1)
a, b := next[0], next[1]
x, y := a*cont[i]+b, a
g := gcd(x, y)
return []int{x / g, y / g}
}
return dfs(0)
}

func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
Loading