Skip to content

补交homework #880

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions Week_02/id_145/LeetCode_236_145.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
27 changes: 27 additions & 0 deletions Week_02/id_145/LeetCode_692_145.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
struct intComp {
bool operator() (const pair<int, string>& lhs, const pair<int, string>& rhs) const{
return lhs.first > rhs.first;
}
};
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
vector<string> resVec(k);//存储结果
map<string, int> hashMap;
//用于统计各个单词出现的次数,并按照字母顺序排序
for (auto word : words) {
hashMap[word] += 1;
}
//然后按照单词出现的次数进行排序
multiset<pair<int, string>, intComp> mySet;
for (auto &item : hashMap) {
mySet.insert({ item.second, item.first });
}
//取出前k个
multiset<pair<int, string>>::iterator it = mySet.begin();
for (int i = 0; i < k; ++i, ++it) {
resVec[i] = it->second;
}
return resVec;
}
};
31 changes: 31 additions & 0 deletions Week_03/id_145/LeetCode_200_145.cpp
Original file line number Diff line number Diff line change
@@ -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<vector<int>> map;
vector<int> tmp;
int numIslands(vector<vector<char>>& grid) {
if(!grid.size()) return 0;
m = grid.size();n = grid[0].size();
for(i=0;i<m;i++) {
tmp.clear();
for(j=0;j<n;j++) tmp.push_back(grid[i][j]-'0');
map.push_back(tmp);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(map[i][j]==1) dfs(i,j,none),index++;
return index-2;
}
void dfs(int i,int j,int flag) { //flag判断来的方向,预防死环
if(i<0||i>m-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);
}
};
33 changes: 33 additions & 0 deletions Week_03/id_145/LeetCode_310_145.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if(n==1) return {0};
vector<unordered_set<int> > adj(n);
vector<int> res;
queue<int> q;
for(auto edge:edges){
adj[edge[0]].insert(edge[1]);
adj[edge[1]].insert(edge[0]);
}
for(int i=0;i<n;i++){
if(adj[i].size()==1) q.push(i);
}
while(n>2){
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;
}
};
58 changes: 58 additions & 0 deletions Week_04/id_145/LeetCode_51_145.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<vector<int>> mark;
vector<string> item;
for(int i = 0; i < n; i ++)
{
mark.push_back(vector<int>());
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<string> &item, vector<vector<string>> &res, vector<vector<int>> &mark)
{
if(k == n)
{
res.push_back(item);
return;
}
for(int i = 0; i < n; i ++)
{
if(mark[k][i] == 0)
{
vector<vector<int>> 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<vector<int>> &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;
}
}
}
};
26 changes: 26 additions & 0 deletions Week_04/id_145/LeetCode_72_145.cpp
Original file line number Diff line number Diff line change
@@ -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<vector<int>> dp(len1+1,vector<int>(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];
}
};