每日5题(2)
(1)01背包问题
#include<iostream> #include<math.h> #include<stdlib.h> using namespace std; const int N = 1010; int n, m, w[N], v[N]; int f[N][N]; int max(int a, int b){ if (a >= b){ return a; } else{ return b; } } int main(){ cin >> n >> m; for (int i = 1; i <= n; i ++ ){ cin >> w[i] >> v[i]; } for (int i = 1; i <= n; i ++ ){ for (int j = 1; j <=m ; j++){ if (j < w[i]){ f[i][j] = f[i - 1][j]; } else{ f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]); } } } cout << f[n][m] << endl; system("pause"); return 0; }
//优化
for (int i = 1; i <= n; i++){
for (int j = m; j > w[i]; j--){
f[j] = max(f[j], f[j - w[i]]+v[i]);
}
}
(2)vector动态数组的初始化
vector<vector<bool> >res(n, vector<bool>(m, false));
初始化一个n*m的动态数组为false
(3)层次遍历二叉树bfs
//bfs层次遍历二叉树 Definition for a binary tree node struct TreeNode{ int val; TreeNode * left; TreeNode * right; TreeNode(int x) :val(x), left(NULL), right(NULL){} }; class Solution{ public: vector<int> printFromTopToBottom(struct TreeNode * root){ vector<int> res; if (!root){ return res; } queue<TreeNode *>q; q.push(root); while (!q.empty()){ TreeNode * rt = q.front(); q.pop(); res.push_back(rt->val); if (rt->left){ q.push(root->left); } if (rt->right){ q.push(root->right); } } return res; } };
(4)分行打印出二叉树的每层元素
//按行打印出二叉树的元素 vector<vector<int> > printFromTopToBottom(TreeNode * root){ vector<vector<int>>res; if (!root){ return res; } queue<TreeNode *>q; vector<int>cur; q.push(root); q.push(NULL); while (q.size()){ TreeNode * rt = q.front(); q.pop(); if (rt){ cur.push_back(rt->val); if (rt->left){ q.push(rt->left); } if (rt->right){ q.push(rt->right); } } else{ if (q.size()){ q.push(NULL); } res.push_back(cur); cur.clear(); } } return res; }
方式二:
vector<vector<int>> printFromToptopBottom(TreeNode * root){ vector<vector<int>>res; if (!root){ return res; } vector<TreeNode *>level; level.push_back(root); res.push_back(get_level(level)); while (true){ vector<TreeNode *>newLevel; for (auto &u : level){ if (u->left){ newLevel.push_back(u->left); } if (u->right){ newLevel.push_back(u->right); } } if (newLevel.size()){ res.push_back(get_level(newLevel)); level = newLevel; } else{ break; } } return res; } vector<int> get_level(vector<TreeNode *> level){ vector<int >res; for (auto &u : level){ res.push_back(u->val); } return res; }
(5)之字形打印二叉树,即先从左往右打印节点,然后从右往左打印节点,再从左往右打印节点。。。。。。
//之字形打印二叉树 vector<int> get_level(vector<TreeNode *> level){ vector<int>res; for (auto &u : level){ res.push_back(u->val); } return res; } vector<vector<int>> printFromToptoBottom(TreeNode * root){ vector<vector<int>>res; if (!root){ return res; } vector<TreeNode *> cur; cur.push_back(root); res.push_back(get_level(cur)); bool flag = true; while (true){ vector<TreeNode *>newCur; for (auto &u : cur){ if (u->left){ newCur.push_back(u->left); } if (u->right){ newCur.push_back(u->right); } } if (newCur.size()){ vector<int>temp = get_level(newCur); if (flag){ reverse(temp.begin(), temp.end()); } res.push_back(temp); cur = newCur; } else{ break; } flag = !flag; } return res; }

浙公网安备 33010602011771号