完全背包
完全背包:每个物品可以使用无数次
01背包核心代码:倒数遍历为了每个物品只使用一次!
for(int i = 0; i < weight.size(); i++) { // 遍历物品
for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
变更为正序遍历则是完全背包:
// 先遍历物品,再遍历背包
for(int i = 0; i < weight.size(); i++) { // 遍历物品
for(int j = weight[i]; j <= bagWeight ; j++) { // 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
完全背包问题的两层for可以颠倒
518. 零钱兑换 II
C++解法
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp (amount+1, 0);
dp[0] = 1;
for (int i = 0; i < coins.size(); i++){
for (int j = coins[i]; j <= amount; j++){
dp[j] += dp[j-coins[i]];
}
}
return dp[amount];
}
};
Python解法
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [0] * (amount+1)
dp[0] = 1
for i in range(len(coins)):
for j in range(coins[i], amount+1):
dp[j] += dp[j-coins[i]]
return dp[amount]
377. 组合总和 Ⅳ
C++解法
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp (target+1, 0);
dp[0] = 1;
for (int i = 0; i <= target; i++){
for (int j = 0; j < nums.size(); j++){
if (i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};
Python解法
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
dp = [0] * (target+1)
dp[0] = 1
for i in range(1, target+1):
for j in nums:
if i >= j:
dp[i] += dp[i - j]
return dp[-1]
Day 15 复习
102.二叉树的层序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void BFS(TreeNode* curr, vector<vector<int>>& result, int depth){
if (curr == nullptr){
return;
}
if (result.size() == depth) result.push_back(vector<int>());
result[depth].push_back(curr->val);
BFS(curr->left, result, depth+1);
BFS(curr->right, result, depth+1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
BFS(root, result, 0);
return result;
}
};
107.二叉树的层次遍历II
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void BSF(TreeNode* curr, vector<vector<int>>& result, int depth){
if (curr == nullptr) return;
if (result.size() == depth) result.push_back(vector<int>());
result[depth].push_back(curr->val);
BSF(curr->left, result, depth+1);
BSF(curr->right, result, depth+1);
}
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
BSF(root, result, 0);
reverse(result.begin(), result.end());
return result;
}
};
199.二叉树的右视图
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void BSF(TreeNode* curr, vector<int>& result, int depth){
if (curr == nullptr) return;
if (result.size() == depth) result.push_back(curr->val);
BSF(curr->right, result, depth+1);
BSF(curr->left, result, depth+1);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
BSF(root, result, 0);
return result;
}
};
637.二叉树的层平均值
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<double> result;
while (!que.empty()){
int size = que.size();
double sum = 0;
TreeNode* node;
for (int i = 0; i < size; i++){
node = que.front();
sum += node->val;
que.pop();
if (node->left){
que.push(node->left);
}
if (node->right){
que.push(node->right);
}
}
result.push_back(sum / size);
}
return result;
}
};
429.N叉树的层序遍历
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
void BSF(Node* curr, vector<vector<int>>& result, int depth){
if (curr == nullptr) return;
if (result.size() == depth) result.push_back(vector<int>());
result[depth].push_back(curr->val);
for (int i = 0; i < curr->children.size(); i++){
if (curr->children[i]){
BSF(curr->children[i], result, depth+1);
}
}
}
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> result;
BSF(root, result, 0);
return result;
}
};
515.在每个树行中找最大值
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<int> result;
while (!que.empty()){
int size = que.size();
int max = INT_MIN;
TreeNode* node;
for (int i = 0; i < size; i++){
node = que.front();
max = max < node->val ? node->val : max;
que.pop();
if (node->left){
que.push(node->left);
}
if (node->right){
que.push(node->right);
}
}
result.push_back(max);
}
return result;
}
};
116.填充每个节点的下一个右侧节点指针
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()){
int size = que.size();
for (int i = 0; i < size; i++){
Node* node = que.front();
que.pop();
if (i != size-1){
node->next = que.front();
}
if (node->left){
que.push(node->left);
}
if (node->right){
que.push(node->right);
}
}
}
return root;
}
};
117.填充每个节点的下一个右侧节点指针II
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()){
int size = que.size();
for (int i = 0; i < size; i++){
Node* node = que.front();
que.pop();
if (i != size-1){
node->next = que.front();
}
if (node->left){
que.push(node->left);
}
if (node->right){
que.push(node->right);
}
}
}
return root;
}
};
104.二叉树的最大深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getDepth(TreeNode* curr){
if (curr == NULL){
return 0;
}
int left_depth = getDepth(curr->left);
int right_depth = getDepth(curr->right);
return 1 + max(left_depth, right_depth);
}
int maxDepth(TreeNode* root) {
return getDepth(root);
}
};
111.二叉树的最小深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getHeight(TreeNode* curr){
if (curr == NULL) return 0;
int left_height = getHeight(curr->left);
int right_height = getHeight(curr->right);
if (curr->left == NULL && curr->right){
return 1 + right_height;
} else if (curr->right == NULL && curr->left){
return 1 + left_height;
} else {
return 1 + min(left_height, right_height);
}
}
int minDepth(TreeNode* root) {
return getHeight(root);
}
};
226.翻转二叉树 (优先掌握递归)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*> st;
if (root != NULL) st.push(root);
while (!st.empty()){
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
if (node->left){
st.push(node->left);
}
if (node->right){
st.push(node->right);
}
}
return root;
}
};
101. 对称二叉树 (优先掌握递归)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right){
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
else if (left->val != right->val) return false;
bool outside = compare(left->left, right->right);
bool inside = compare(left->right, right->left);
return inside && outside;
}
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
return compare(root->left, root->right);
}
};