動態規劃:
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;}
};