diff --git "a/topic04/submit/LC310_\346\235\250\347\216\211\345\256\235.cpp" "b/topic04/submit/LC310_\346\235\250\347\216\211\345\256\235.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..641edad1e9edc156d7354fce73419a5581d44ef7 --- /dev/null +++ "b/topic04/submit/LC310_\346\235\250\347\216\211\345\256\235.cpp" @@ -0,0 +1,66 @@ +class Solution +{ +public: + vector findMinHeightTrees(int n, vector>& edges) + { + if (n == 1) + { + return { 0 }; + } + + // 构建邻接表,所有和 i 节点相连的点都会放入 + unordered_map> adj; + for (int i = 0; i < n; ++i) + { + adj[i] = {}; + } + + // 记录不同节点邻居的数量 + vector neighborNum(n, 0); + for (const auto& edge : edges) + { + adj[edge[0]].push_back(edge[1]); + adj[edge[1]].push_back(edge[0]); + neighborNum[edge[0]]++; + neighborNum[edge[1]]++; + } + + // 初始化队列,包含所有度为 1 的节点(只有 1 个相邻节点的节点,也就是叶子节点) + queue queue; + for (int i = 0; i < n; ++i) + { + if (neighborNum[i] == 1) + { + queue.push(i); + } + } + + // 每层叶子节点加入的结果集 + vector result; + while (!queue.empty()) + { + // 上一层叶子节点加入的结果集直接清空,我们只需要最后一层的叶子节点即可 + result.clear(); + int size = queue.size(); + for (int i = 0; i < size; i++) + { + // 叶子节点出队列并加进结果集 + int leafNode = queue.front(); + queue.pop(); + result.push_back(leafNode); + for (int neighbor : adj[leafNode]) + { + // 每剥离一个叶子节点,这个叶子节点邻居的邻居节点数量减一 + // 当这个叶子节点邻居数量为 1的时候,外层叶子节点全剥完了,这个邻居节点也成为了叶子节点,加入队列 + // adj无需去除相邻节点,因为只判断到 1 就入队列,neighborNum[neighbor]重复减(减到1以下)不影响结果 + neighborNum[neighbor]--; + if (neighborNum[neighbor] == 1) + { + queue.push(neighbor); + } + } + } + } + return result; + } +}; diff --git "a/topic04/submit/LC_687\346\235\250\347\216\211\345\256\235.cpp" "b/topic04/submit/LC_687\346\235\250\347\216\211\345\256\235.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8f17833440fed148eee44154308768fcacf2bd97 --- /dev/null +++ "b/topic04/submit/LC_687\346\235\250\347\216\211\345\256\235.cpp" @@ -0,0 +1,17 @@ +class Solution { +public: + int ans; + int dfs(TreeNode* root) { + if (!root)return 0; + const int l = dfs(root->left), r = dfs(root->right); + const int lp = (root->left && root->left->val == root->val) ? l : 0; + const int rp = (root->right && root->right->val == root->val) ? r : 0; + ans = max(ans, lp + rp); + return max(lp, rp) + 1; + } + int longestUnivaluePath(TreeNode* root) { + ans = 0; + dfs(root); + return ans; + } +}; \ No newline at end of file diff --git "a/topic05/submit/LC343_\346\235\250\347\216\211\345\256\235.cpp" "b/topic05/submit/LC343_\346\235\250\347\216\211\345\256\235.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0f5f878cc850e23af16f7d8888ee65872f7604b7 --- /dev/null +++ "b/topic05/submit/LC343_\346\235\250\347\216\211\345\256\235.cpp" @@ -0,0 +1,27 @@ +class Solution { +public: + int integerBreak(int n) { + //完全背包 + vector memo(n + 1, vector(n + 1, -1)); + return backTrack(memo, n - 1, n); + } + int backTrack(vector>& memo, int i, int c) + { + if (i <= 0) + { + return 1; + } + if (memo[i][c] != -1) + { + return memo[i][c]; + } + if (c < i) + { + return memo[i][c] = backTrack(memo, i - 1, c); + } + else + { + return memo[i][c] = max(backTrack(memo, i - 1, c), backTrack(memo, i, c - i) * i); + } + } +}; diff --git "a/topic05/submit/LC70_\346\235\250\347\216\211\345\256\235.cpp" "b/topic05/submit/LC70_\346\235\250\347\216\211\345\256\235.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c8d19ceacffb0cd8d57b5f4b20ee6a0b5d803364 --- /dev/null +++ "b/topic05/submit/LC70_\346\235\250\347\216\211\345\256\235.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + int climbStairs(int n) { + //因为有n阶台阶,台阶从0开始计算,所以定义n+1个元素 + vector dp(n + 1, 0); + //定义边界dp[0]=1 + dp[0] = 1; + dp[1] = 1; + for (int i = 2; i <= n; ++i) { + //状态转移公式 + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +};