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 labuladong:master #101

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 1 commit into AlgorithmAndLeetCode:master from labuladong:master
Jun 15, 2023
Merged
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
42 changes: 23 additions & 19 deletions 多语言解法代码/solution_code.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28924,27 +28924,31 @@ class Solution {
```javascript
// by chatGPT (javascript)
var rob = function(root) {
// memo 需要提到函数外侧,否则每次递归重新声明 memo,相当于备忘录没有生效
const memo = new Map();
if (root === null) {
return 0;
let dp = (node) => {
if (node === null) {
return 0;
}
// 利用备忘录消除重叠子问题
if (memo.has(node)) {
return memo.get(node);
}
// 抢,然后去下下家
let do_it = node.val +
((node.left === null) ?
0 :
dp(node.left.left) + dp(node.left.right)) +
((node.right === null) ?
0 :
dp(node.right.left) + dp(node.right.right));
// 不抢,然后去下家
let not_do = dp(node.left) + dp(node.right);
let res = Math.max(do_it, not_do);
memo.set(node, res);
return res;
}
// 利用备忘录消除重叠子问题
if (memo.has(root)) {
return memo.get(root);
}
// 抢,然后去下下家
let do_it = root.val +
((root.left === null) ?
0 :
rob(root.left.left) + rob(root.left.right)) +
((root.right === null) ?
0 :
rob(root.right.left) + rob(root.right.right));
// 不抢,然后去下家
let not_do = rob(root.left) + rob(root.right);
let res = Math.max(do_it, not_do);
memo.set(root, res);
return res;
return dp(root)
};
```

Expand Down

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