算法 day9
长按键入
class Solution { public: bool isLongPressedName(string name, string typed) { int p1 = 0; int p2 = 0; int len1 = name.length(); int len2 = typed.length(); while(p1<len1&&p1<len2) { char nowc = name[p1]; int c1 = 0; while(name[p1] == nowc) { p1++; c1++; } int c2 = 0; while(typed[p2] == nowc) { p2++; c2++; } if(c1>c2)return false; } if(p1==len1&&p2==len2)return true; else return false; } };
墙与门
多源bfs重写
class Solution { public: void wallsAndGates(vector<vector<int>>& rooms) { queue<pair<int,int>>q; int n = rooms.size(); if(!n)return; int m = rooms[0].size(); int inf = 2147483647; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(rooms[i][j]==0)q.push({i,j}); } } while(!q.empty()) { int dx[] = {-1,1,0,0}; int dy[] = {0,0,-1,1}; pair<int,int>tmp = q.front(); q.pop(); for(int k=0;k<4;k++) { int newx = tmp.first+dx[k]; int newy = tmp.second+dy[k]; if(newx>=0&&newx<n&&newy>=0&&newy<m&&rooms[newx][newy]==inf) { rooms[newx][newy] = rooms[tmp.first][tmp.second]+1; q.push({newx,newy}); } } } } };
岛屿数量
class Solution { public: int numIslands(vector<vector<char>>& grid) { int n = grid.size(); if(!n)return 0; int m = grid[0].size(); queue<pair<int,int>>q; int ccount = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(grid[i][j] == '1') { grid[i][j] = '0'; q.push({i,j}); ccount++; while(!q.empty()) { pair<int,int>tmp = q.front(); q.pop(); int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; for(int k=0;k<4;k++) { int newx = tmp.first+dx[k]; int newy = tmp.second+dy[k]; if(newx>=0&&newx<n&&newy>=0&&newy<m&&grid[newx][newy]=='1') { grid[newx][newy] ='0'; q.push({newx,newy}); } } } } } } return ccount; } };
两个二叉搜索树节点最深公共祖先
/** * 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) { int pval = p->val; int qval = q->val; int rootval = root->val; printf("%d\n",rootval); if(root==NULL)return root; if((qval-rootval)*(pval-rootval)<=0) return root; if(qval>rootval) return lowestCommonAncestor(root->right,p,q); if(qval<rootval) return lowestCommonAncestor(root->left,p,q); return root; } };
两个二叉树节点最深公共祖先
/** * 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) { if(root == NULL || p==root || q==root) return root; TreeNode* left = lowestCommonAncestor(root->left,p,q); TreeNode* right = lowestCommonAncestor(root->right,p,q); if(left !=NULL && right!=NULL)return root; if(left == NULL)return right; else return left; } };
机器人
class Solution { public: int uniquePaths(int m, int n) { int dp[m+1][n+1]; //求dp[m][n] for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(i==1||j==1) { dp[i][j]=1; } else { dp[i][j]=0; } } } for(int i=2;i<=m;i++) { for(int j=2;j<=n;j++) { dp[i][j]=dp[i-1][j]+dp[i][j-1]; } } return dp[m][n]; } };
最小路径和
class Solution { public: int minPathSum(vector<vector<int>>& grid) { int m = grid.size(); if(!m)return 0; int n = grid[0].size(); int dp[m][n]; dp[0][0] = grid[0][0]; for(int i=1;i<m;i++) { dp[i][0] = dp[i-1][0]+grid[i][0]; } for(int j=1;j<n;j++) { dp[0][j] = dp[0][j-1]+grid[0][j]; } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { dp[i][j] = grid[i][j]+min(dp[i-1][j],dp[i][j-1]); } } return dp[m-1][n-1]; } };