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 #479

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
jenningsloy318 merged 2 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 27, 2024
Merged
Changes from 1 commit
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
Next Next commit
416.εˆ†ε‰²η­‰ε’Œε­ι›†ε’žεŠ Go二维dp解法
  • Loading branch information
markwang1992 committed Aug 14, 2024
commit 247cfabf08d2e281fd32c65eac6c620633a3e64d
39 changes: 39 additions & 0 deletions problems/0416.εˆ†ε‰²η­‰ε’Œε­ι›†.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class Solution:
```

### Go:
δΈ€η»΄dp
```go
// εˆ†ε‰²η­‰ε’Œε­ι›† εŠ¨ζ€θ§„εˆ’
// 既间倍杂度O(n^2) 空间倍杂度O(n)
Expand Down Expand Up @@ -480,6 +481,44 @@ func canPartition(nums []int) bool {
}
```

二维dp
```go
func canPartition(nums []int) bool {
sum := 0
for _, val := range nums {
sum += val
}
if sum % 2 == 1 {
return false
}
target := sum / 2
dp := make([][]int, len(nums))
for i := range dp {
dp[i] = make([]int, target + 1)
}
for j := nums[0]; j <= target; j++ {
dp[0][j] = nums[0]
}
for i := 1; i < len(nums); i++ {
for j := 0; j <= target; j++ {
if j < nums[i] {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = max(dp[i-1][j], dp[i-1][j-nums[i]] + nums[i])
}
}
}
return dp[len(nums)-1][target] == target
}

func max(x, y int) int {
if x > y {
return x
}
return y
}
```

### JavaScript:

```js
Expand Down

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /