From f4b656b774e93953efc86600638e22f674d31c7b Mon Sep 17 00:00:00 2001 From: liuhe666 Date: Sun, 19 May 2019 20:53:30 +0800 Subject: [PATCH] =?UTF-8?q?week2=E3=80=81week3=E3=80=81week4=20home=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_145/LeetCode_236_145.cpp | 27 ++++++++++++++ Week_02/id_145/LeetCode_692_145.cpp | 27 ++++++++++++++ Week_03/id_145/LeetCode_200_145.cpp | 31 +++++++++++++++ Week_03/id_145/LeetCode_310_145.cpp | 33 ++++++++++++++++ Week_04/id_145/LeetCode_51_145.cpp | 58 +++++++++++++++++++++++++++++ Week_04/id_145/LeetCode_72_145.cpp | 26 +++++++++++++ 6 files changed, 202 insertions(+) create mode 100644 Week_02/id_145/LeetCode_236_145.cpp create mode 100644 Week_02/id_145/LeetCode_692_145.cpp create mode 100644 Week_03/id_145/LeetCode_200_145.cpp create mode 100644 Week_03/id_145/LeetCode_310_145.cpp create mode 100644 Week_04/id_145/LeetCode_51_145.cpp create mode 100644 Week_04/id_145/LeetCode_72_145.cpp diff --git a/Week_02/id_145/LeetCode_236_145.cpp b/Week_02/id_145/LeetCode_236_145.cpp new file mode 100644 index 00000000..69fb8af7 --- /dev/null +++ b/Week_02/id_145/LeetCode_236_145.cpp @@ -0,0 +1,27 @@ +/** + * 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: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + /** + 注意p,q必然存在树内, 且所有节点的值唯一!!! + 递归思想, 对以root为根的(子)树进行查找p和q, 如果root == null || p || q 直接返回root + 表示对于当前树的查找已经完毕, 否则对左右子树进行查找, 根据左右子树的返回值判断: + 1. 左右子树的返回值都不为null, 由于值唯一左右子树的返回值就是p和q, 此时root为LCA + 2. 如果左右子树返回值只有一个不为null, 说明只有p和q存在与左或右子树中, 最先找到的那个节点为LCA + 3. 左右子树返回值均为null, p和q均不在树中, 返回null + **/ + if (!root || root == p || root == q) return root; + auto left = lowestCommonAncestor(root->left, p, q); + auto right = lowestCommonAncestor(root->right, p, q); + if (left && right) return root; + return left ? left : right; + } +}; \ No newline at end of file diff --git a/Week_02/id_145/LeetCode_692_145.cpp b/Week_02/id_145/LeetCode_692_145.cpp new file mode 100644 index 00000000..a650d6aa --- /dev/null +++ b/Week_02/id_145/LeetCode_692_145.cpp @@ -0,0 +1,27 @@ +struct intComp { + bool operator() (const pair& lhs, const pair& rhs) const{ + return lhs.first > rhs.first; + } +}; +class Solution { +public: + vector topKFrequent(vector& words, int k) { + vector resVec(k);//存储结果 + map hashMap; + //用于统计各个单词出现的次数,并按照字母顺序排序 + for (auto word : words) { + hashMap[word] += 1; + } + //然后按照单词出现的次数进行排序 + multiset, intComp> mySet; + for (auto &item : hashMap) { + mySet.insert({ item.second, item.first }); + } + //取出前k个 + multiset>::iterator it = mySet.begin(); + for (int i = 0; i < k; ++i, ++it) { + resVec[i] = it->second; + } + return resVec; + } +}; \ No newline at end of file diff --git a/Week_03/id_145/LeetCode_200_145.cpp b/Week_03/id_145/LeetCode_200_145.cpp new file mode 100644 index 00000000..493559c1 --- /dev/null +++ b/Week_03/id_145/LeetCode_200_145.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int i,j,m,n,index=2;//index用于标记不同岛序号 + const int left = 0;const int right = 1; //方向常量设置 + const int up = 2;const int down = 3; + const int none = 4; + vector> map; + vector tmp; + int numIslands(vector>& grid) { + if(!grid.size()) return 0; + m = grid.size();n = grid[0].size(); + for(i=0;im-1||j<0||j>n-1) return; + if(map[i][j]!=1) return; + map[i][j] = index; + if(flag!=right) dfs(i,j+1,left); + if(flag!=left) dfs(i,j-1,right); + if(flag!=up) dfs(i+1,j,down); + if(flag!=down) dfs(i-1,j,up); + } +}; \ No newline at end of file diff --git a/Week_03/id_145/LeetCode_310_145.cpp b/Week_03/id_145/LeetCode_310_145.cpp new file mode 100644 index 00000000..a6b2b153 --- /dev/null +++ b/Week_03/id_145/LeetCode_310_145.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + if(n==1) return {0}; + vector > adj(n); + vector res; + queue q; + for(auto edge:edges){ + adj[edge[0]].insert(edge[1]); + adj[edge[1]].insert(edge[0]); + } + for(int i=0;i2){ + int size=q.size(); + n-=size; + while(size-->0){ + auto t=q.front(); + q.pop(); + for(auto itm:adj[t]){ + adj[itm].erase(t); + if(adj[itm].size()==1) q.push(itm); + } + } + } + while(!q.empty()){ + res.push_back(q.front()); + q.pop(); + } + return res; + } +}; \ No newline at end of file diff --git a/Week_04/id_145/LeetCode_51_145.cpp b/Week_04/id_145/LeetCode_51_145.cpp new file mode 100644 index 00000000..d62d1998 --- /dev/null +++ b/Week_04/id_145/LeetCode_51_145.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + vector> solveNQueens(int n) { + vector> res; + vector> mark; + vector item; + for(int i = 0; i < n; i ++) + { + mark.push_back(vector()); + for(int j = 0; j < n; j ++) + { + mark[i].push_back(0); + } + item.push_back(""); + item[i].append(n,'.');//添加n个'.' + } + generate(0, n, item, res, mark); + return res; + } + //回溯法 + void generate(int k, int n, vector &item, vector> &res, vector> &mark) + { + if(k == n) + { + res.push_back(item); + return; + } + for(int i = 0; i < n; i ++) + { + if(mark[k][i] == 0) + { + vector> tmp_mark = mark; + item[k][i] = 'Q'; + put_down_the_queen(k, i, mark);//更新mark矩阵 + generate(k + 1, n, item, res, mark); + mark = tmp_mark; + item[k][i] = '.'; + } + } + } + //放下queen后更新mark矩阵 + void put_down_the_queen(int x, int y, vector> &mark) + { + static const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}; + static const int dy[] = {1, 1, 1, 0, 0, -1, -1, -1}; + mark[x][y] = 1; + for(int i = 1; i < mark.size(); i ++) + { + for(int j = 0; j < 8; j ++) + { + int new_x = x + i * dx[j]; + int new_y = y + i * dy[j]; + if(new_x >= 0 && new_x < mark.size() && new_y >= 0 && new_y < mark.size()) + mark[new_x][new_y] = 1; + } + } + } +}; \ No newline at end of file diff --git a/Week_04/id_145/LeetCode_72_145.cpp b/Week_04/id_145/LeetCode_72_145.cpp new file mode 100644 index 00000000..ad07d37b --- /dev/null +++ b/Week_04/id_145/LeetCode_72_145.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minDistance(string word1, string word2) { + int len1=word1.length(),len2=word2.length(); + //dp[index1][index2]:前index1个word1字符转换成前index2个word2字符需要的最少步数 + vector> dp(len1+1,vector(len2+1)); + for(int index1=0;index1<=len1;index1++){ + for(int index2=0;index2<=len2;index2++){ + if(index1 == 0) + //从无到有显然要经历index2步插入操作 + dp[index1][index2] = index2; + else if(index2 == 0) + //从有到无显然要经历index1步删除操作 + dp[index1][index2] = index1; + //三种操作方式 + //1.word1删除末尾字符 dp[index1-1][index2]+1 + //2.word1在末尾插入字符 dp[index1][index2-1]+1 + //3.word1替换末尾字符(如果末尾字符本来就相等,就不用换了) dp[index1-1][index2-1]+(word1[index1-1]!=word2[index2-1] + else dp[index1][index2] = min(min(dp[index1-1][index2]+1,dp[index1][index2-1]+1),dp[index1-1][index2-1]+(word1[index1-1]!=word2[index2-1])); + //cout << dp[index1][index2] << " "; + } + //cout << endl; + } + return dp[len1][len2]; + } +}; \ No newline at end of file