Skip to content

[pull] main from doocs:main #503

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

Merged
merged 5 commits into from
Mar 28, 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
21 changes: 21 additions & 0 deletions solution/0100-0199/0169.Majority Element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
function majorityElement($nums) {
$major = 0;
$count = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($count == 0) $major = $nums[$i];
if ($major == $nums[$i]) $count++;
else $count--;
}
return $major;
}
}
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions solution/0100-0199/0169.Majority Element/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
function majorityElement($nums) {
$major = 0;
$count = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($count == 0) $major = $nums[$i];
if ($major == $nums[$i]) $count++;
else $count--;
}
return $major;
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0100-0199/0169.Majority Element/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
function majorityElement($nums) {
$major = 0;
$count = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($count == 0) $major = $nums[$i];
if ($major == $nums[$i]) $count++;
else $count--;
}
return $major;
}
}
141 changes: 140 additions & 1 deletion solution/0800-0899/0842.Split Array into Fibonacci Sequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,161 @@

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

**方法一:回溯 + 剪枝**

我们设计一个函数 $dfs(i)$,表示从字符串 $num$ 的第 $i$ 个字符开始拆分,拆分出的斐波那契式序列是否满足题目要求。如果满足,我们就返回 $true$,否则返回 $false$。

函数 $dfs(i)$ 的具体实现如下:

如果 $i$ 等于字符串 $num$ 的长度,说明我们已经拆分完整个字符串,此时我们只需要判断拆分出的序列的长度是否大于 $2$ 即可。如果大于 $2$,说明我们找到了一组满足题目要求的斐波那契式序列,返回 $true$;否则返回 $false$。

如果 $i$ 小于字符串 $num$ 的长度,我们需要枚举拆分出的第一个数 $x$,如果 $x$ 的长度大于 $1$,且以 $0$ 开头,说明 $x$ 不是一个合法的数,我们直接返回 $false$。否则我们将 $x$ 转换成十进制数,如果 $x$ 大于 $2^{31} - 1$,或者 $x$ 大于 $ans$ 的最后两个数之和,直接返回 $false$。如果 $ans$ 的长度小于 $2$,或者 $x$ 等于 $ans$ 的最后两个数之和,我们将 $x$ 加入到 $ans$ 中,然后继续拆分字符串 $num$ 的后面的部分,如果返回 $true$,说明我们找到了一组满足题目要求的斐波那契式序列,返回 $true$;否则我们将 $x$ 从 $ans$ 中移除,然后继续枚举拆分出的第一个数 $x$。

时间复杂度 $O(n \times \log^2 M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是字符串 $num$ 的长度和整型数的最大值。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def splitIntoFibonacci(self, num: str) -> List[int]:
def dfs(i):
if i == n:
return len(ans) > 2
x = 0
for j in range(i, n):
if j > i and num[i] == '0':
break
x = x * 10 + int(num[j])
if x > 2**31 - 1 or (len(ans) > 2 and x > ans[-2] + ans[-1]):
break
if len(ans) < 2 or ans[-2] + ans[-1] == x:
ans.append(x)
if dfs(j + 1):
return True
ans.pop()
return False

n = len(num)
ans = []
dfs(0)
return ans
```

### **Java**

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

```java
class Solution {
private List<Integer> ans = new ArrayList<>();
private String num;

public List<Integer> splitIntoFibonacci(String num) {
this.num = num;
dfs(0);
return ans;
}

private boolean dfs(int i) {
if (i == num.length()) {
return ans.size() >= 3;
}
long x = 0;
for (int j = i; j < num.length(); ++j) {
if (j > i && num.charAt(i) == '0') {
break;
}
x = x * 10 + num.charAt(j) - '0';
if (x > Integer.MAX_VALUE || (ans.size() >= 2 && x > ans.get(ans.size() - 1) + ans.get(ans.size() - 2))) {
break;
}
if (ans.size() < 2 || x == ans.get(ans.size() - 1) + ans.get(ans.size() - 2)) {
ans.add((int) x);
if (dfs(j + 1)) {
return true;
}
ans.remove(ans.size() - 1);
}
}
return false;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> splitIntoFibonacci(string num) {
int n = num.size();
vector<int> ans;
function<bool(int)> dfs = [&](int i) -> bool {
if (i == n) {
return ans.size() > 2;
}
long long x = 0;
for (int j = i; j < n; ++j) {
if (j > i && num[i] == '0') {
break;
}
x = x * 10 + num[j] - '0';
if (x > INT_MAX || (ans.size() > 1 && x > (long long) ans[ans.size() - 1] + ans[ans.size() - 2])) {
break;
}
if (ans.size() < 2 || x == (long long) ans[ans.size() - 1] + ans[ans.size() - 2]) {
ans.push_back(x);
if (dfs(j + 1)) {
return true;
}
ans.pop_back();
}
}
return false;
};
dfs(0);
return ans;
}
};
```

### **Go**

```go
func splitIntoFibonacci(num string) []int {
n := len(num)
ans := []int{}
var dfs func(int) bool
dfs = func(i int) bool {
if i == n {
return len(ans) > 2
}
x := 0
for j := i; j < n; j++ {
if j > i && num[i] == '0' {
break
}
x = x*10 + int(num[j]-'0')
if x > math.MaxInt32 || (len(ans) > 1 && x > ans[len(ans)-1]+ans[len(ans)-2]) {
break
}
if len(ans) < 2 || x == ans[len(ans)-1]+ans[len(ans)-2] {
ans = append(ans, x)
if dfs(j + 1) {
return true
}
ans = ans[:len(ans)-1]
}
}
return false
}
dfs(0)
return ans
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,140 @@
### **Python3**

```python

class Solution:
def splitIntoFibonacci(self, num: str) -> List[int]:
def dfs(i):
if i == n:
return len(ans) > 2
x = 0
for j in range(i, n):
if j > i and num[i] == '0':
break
x = x * 10 + int(num[j])
if x > 2**31 - 1 or (len(ans) > 2 and x > ans[-2] + ans[-1]):
break
if len(ans) < 2 or ans[-2] + ans[-1] == x:
ans.append(x)
if dfs(j + 1):
return True
ans.pop()
return False

n = len(num)
ans = []
dfs(0)
return ans
```

### **Java**

```java
class Solution {
private List<Integer> ans = new ArrayList<>();
private String num;

public List<Integer> splitIntoFibonacci(String num) {
this.num = num;
dfs(0);
return ans;
}

private boolean dfs(int i) {
if (i == num.length()) {
return ans.size() >= 3;
}
long x = 0;
for (int j = i; j < num.length(); ++j) {
if (j > i && num.charAt(i) == '0') {
break;
}
x = x * 10 + num.charAt(j) - '0';
if (x > Integer.MAX_VALUE || (ans.size() >= 2 && x > ans.get(ans.size() - 1) + ans.get(ans.size() - 2))) {
break;
}
if (ans.size() < 2 || x == ans.get(ans.size() - 1) + ans.get(ans.size() - 2)) {
ans.add((int) x);
if (dfs(j + 1)) {
return true;
}
ans.remove(ans.size() - 1);
}
}
return false;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> splitIntoFibonacci(string num) {
int n = num.size();
vector<int> ans;
function<bool(int)> dfs = [&](int i) -> bool {
if (i == n) {
return ans.size() > 2;
}
long long x = 0;
for (int j = i; j < n; ++j) {
if (j > i && num[i] == '0') {
break;
}
x = x * 10 + num[j] - '0';
if (x > INT_MAX || (ans.size() > 1 && x > (long long) ans[ans.size() - 1] + ans[ans.size() - 2])) {
break;
}
if (ans.size() < 2 || x == (long long) ans[ans.size() - 1] + ans[ans.size() - 2]) {
ans.push_back(x);
if (dfs(j + 1)) {
return true;
}
ans.pop_back();
}
}
return false;
};
dfs(0);
return ans;
}
};
```

### **Go**

```go
func splitIntoFibonacci(num string) []int {
n := len(num)
ans := []int{}
var dfs func(int) bool
dfs = func(i int) bool {
if i == n {
return len(ans) > 2
}
x := 0
for j := i; j < n; j++ {
if j > i && num[i] == '0' {
break
}
x = x*10 + int(num[j]-'0')
if x > math.MaxInt32 || (len(ans) > 1 && x > ans[len(ans)-1]+ans[len(ans)-2]) {
break
}
if len(ans) < 2 || x == ans[len(ans)-1]+ans[len(ans)-2] {
ans = append(ans, x)
if dfs(j + 1) {
return true
}
ans = ans[:len(ans)-1]
}
}
return false
}
dfs(0)
return ans
}
```

### **...**
Expand Down
Loading