Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #62

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
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 20, 2022
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
18 changes: 18 additions & 0 deletions problems/0096.不同的二叉搜索树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ function numTrees(n: number): number {
};
```

### Rust

```Rust
impl Solution {
pub fn num_trees(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[0] = 1;
for i in 1..=n {
for j in 1..=i {
dp[i] += dp[j - 1] * dp[i - j];
}
}
dp[n]
}
}
```

### C

```c
Expand Down
21 changes: 21 additions & 0 deletions problems/0343.整数拆分.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,27 @@ function integerBreak(n: number): number {
};
```

### Rust

```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32{
if a > b { a } else { b }
}
pub fn integer_break(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[2] = 1;
for i in 3..=n {
for j in 1..i - 1 {
dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32));
}
}
dp[n]
}
}
```

### C

```c
Expand Down
64 changes: 50 additions & 14 deletions problems/0376.摆动序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,58 @@ class Solution:

### Go

**贪心**
```golang
func wiggleMaxLength(nums []int) int {
var count,preDiff,curDiff int
count=1
if len(nums)<2{
return count
}
for i:=0;i<len(nums)-1;i++{
curDiff=nums[i+1]-nums[i]
//如果有正有负则更新下标值||或者只有前一个元素为0(针对两个不等元素的序列也视作摆动序列,且摆动长度为2)
if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){
preDiff=curDiff
count++
}
}
return count
var count, preDiff, curDiff int //初始化默认为0
count = 1 // 初始化为1,因为最小的序列是1个数
if len(nums) < 2 {
return count
}
for i := 0; i < len(nums)-1; i++ {
curDiff = nums[i+1] - nums[i]
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
count++
}
}
return count
}
```

**动态规划**
```golang
func wiggleMaxLength(nums []int) int {
n := len(nums)
if n <= 1 {
return n
}
dp := make([][2]int, n)
// i 0 作为波峰的最大长度
// i 1 作为波谷的最大长度
dp[0][0] = 1
dp[0][1] = 1
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if nums[j] > nums[i] { //nums[i]为波谷
dp[i][1] = max(dp[i][1], dp[j][0]+1)
}
if nums[j] < nums[i] { //nums[i]为波峰 或者相等
dp[i][0] = max(dp[i][0], dp[j][1]+1)
}
if nums[j] == nums[i] { //添加一种情况,nums[i]为相等
dp[i][0] = max(dp[i][0], dp[j][0]) //波峰
dp[i][1] = max(dp[i][1], dp[j][1]) //波谷
}
}
}
return max(dp[n-1][0], dp[n-1][1])
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
```

Expand Down

AltStyle によって変換されたページ (->オリジナル) /