动态规划:
72. Edit Distance
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
}
}
}
return dp[word1.size()][word2.size()];
}
};
这里的增的功能用另一个删来代替
647. Palindromic Substrings
class Solution {
public:
int countSubstrings(string s) {
vector<vector<bool>> dp(s.size(), vector<bool>(s.size(), false));
int res = 0;
for(int i=s.size()-1; i>=0; i--){
for(int j=i; j<s.size(); j++){
if(s[i] == s[j]){
if(j-i<= 1){
res++;
dp[i][j] = true;
}else if(dp[i+1][j-1]){
res++;
dp[i][j] = true;
}
}
}
}
return res;
}
};
1.dp[i][j]这里要设置为在范围[i,j]范围内是否是回文串
2.范围,因为这里依赖于dp[i+1][j-1]所以一个从大到小,一个从小到大
(完全看答案的,之后要重新写)
516. Longest Palindromic Subsequence
class Solution {
public:
int longestPalindromeSubseq(string s) {
vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));
for(int i=0; i<s.size(); i++) dp[i][i] = 1;
for(int i=s.size()-1; i>=0; i--){
for(int j=i+1; j<s.size(); j++){
if(s[i] == s[j]){
dp[i][j] = dp[i+1][j-1] + 2;
}else{
dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][s.size()-1];
}
};
1.初始化问题:
因为我们设定的是j=i+1, 所以会算不到i=j的时候,所以要初始化为1
2.当不等的时候,要分别看加进来i,j的情况,而不是直接dp[i][j] = dp[i-1][j-1]
二叉树:
199. Binary Tree Right Side View
1.BFS
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
vector<int> res;
if(root != NULL) {
que.push(root);
}
while(!que.empty()){
int size = que.size();
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
if(i == size-1){
res.push_back(cur->val);
}
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return res;
}
};
2.dfs
class Solution {
public:
vector<int> res;
int depth = 0;
vector<int> rightSideView(TreeNode* root) {
traversal(root);
return res;
}
void traversal(TreeNode* cur){
if(cur == NULL) return;
depth++;
if(res.size() < depth){
res.push_back(cur->val);
}
traversal(cur->right);
traversal(cur->left);
depth--;
}
};
226. Invert Binary Tree
1.分解问题
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return NULL;
TreeNode* left = invertTree(root->left);
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
2.遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
traversal(root);
return root;
}
void traversal(TreeNode* cur){
if(cur == NULL) return;
TreeNode* left = cur->left;
cur->left = cur->right;
cur->right = left;
traversal(cur->left);
traversal(cur->right);
}
};
101. Symmetric Tree
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right){
if(left != NULL && right == NULL) return false;
else if(left == NULL && right != NULL) return false;
else if(left == NULL && right == NULL) return true;
else if(left->val != right->val) return false;
bool outside = compare(left->left, right->right);
bool inside = compare(left->right, right->left);
bool isSame = outside && inside;
return isSame;
}
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
return compare(root->left, root->right);
}
};
104. Maximum Depth of Binary Tree
1.遍历思想
class Solution {
public:
int res = 0;
int depth = 0;
int maxDepth(TreeNode* root) {
traversal(root);
return res;
}
void traversal(TreeNode* cur){
if(cur == NULL) return;
depth++;
res = max(res, depth);
traversal(cur->left);
traversal(cur->right);
depth--;
}
};
2.分解问题
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left, right) +1;
}
};