文章分类 - leetcode刷题小记
2022年春招开始记~把平时笔记上记得东西搬过来
摘要:583. 两个字符串的删除操作 class Solution { public: int minDistance(string word1, string word2) { int len1 = word1.size(); int len2 = word2.size(); int dp[len1 +
阅读全文
摘要:309. 最佳买卖股票时机含冷冻期 class Solution { public: int maxProfit(vector<int>& prices) { int p_len = prices.size(); int dp[p_len][3]; memset(dp, 0 ,sizeof(dp))
阅读全文
摘要:416. 分割等和子集(背包) class Solution { public: // dp[i][j] 能获得的最大价值: // i --> 代表前i件商品 // j --> 能得到的重量j // 0/1背包状态转移方程: // dp[i][j] = max(dp[i-1][j-w[i]] + v
阅读全文
摘要:状态转移方程: class Solution { public: int longestCommonSubsequence(string text1, string text2) { int len1 = text1.size(); int len2 = text2.size(); int dp[l
阅读全文
摘要:按照算法导论的说法,如果把每一个数对理解为一个活动的开始和结束时间,那么我们的目的是尽可能多的进行更多的活动。 此时对于贪心算法,我么直观上应该选择这样一个活动,选出它后剩下的资源应能被尽量多的其他任务所用。因此,应该选择最早结束的活动,因为他剩下的资源可供他之后尽量多的活动使用。这也是为什么要按照
阅读全文
摘要:最长递增子序列 // 方法一:动态规划O(N^2) class Solution { public: int lengthOfLIS(vector<int>& nums) { int num_len = nums.size(); int dp[num_len]; dp[0] = 1; int res
阅读全文
摘要:343. 整数拆分 class Solution { public: int integerBreak(int n) { if(n <= 2) return 1; int dp[n+1]; dp[1] = 1; dp[2] = 1; for(int i=3;i<=n;++i){ dp[i] = 1;
阅读全文
摘要:64. 最小路径和 class Solution { public: int minPathSum(vector<vector<int>>& grid) { int len_x = grid.size(); int len_y = grid[0].size(); int dp[len_y]; dp[
阅读全文
摘要:【动态规划】198. 打家劫舍 213. 打家劫舍 II class Solution { public: int rob(vector<int>& nums) { int num_len = nums.size(); if(num_len == 1) return nums[0]; else if
阅读全文
摘要:class Solution { public: void dfs(int index, vector<string> &cur_board){ if (index == nn){ result.push_back(cur_board); } else{ int x, y = index; for(
阅读全文
摘要:位运算可以大大降低运算难度!!! class Solution { public: void print(vector<vector<char>>& board){ for(auto b: board){ for(auto a: b){ cout << a << " "; } cout << end
阅读全文
摘要:78. 子集 class Solution { public: void dfs(vector<int>& nums, vector<int> &cur, int index){ if(index > num_len) return; result.push_back(cur); for(int i
阅读全文
摘要:组合总和 class Solution { public: void dfs(vector<int>& candidates, vector<int> &cur, int index, int sum){ if(sum == target) result.push_back(cur); else{
阅读全文
摘要:未优化剪枝版: class Solution { public: void dfs(vector<bool> &record, vector<int> &cur, int index){ if(cur.size() == kk) result.emplace_back(cur); else{ for
阅读全文
摘要:注意是对原数组进行交换!!! class Solution { public: void dfs(vector<int>& nums, int index){ if(index == num_len){ result.push_back(nums); return; } for(int i=inde
阅读全文
摘要:class Solution { public: bool is_get(int x, int y){ if(x >= 0 && x < len_x && y >= 0 && y < len_y) return true; return false; } int dfs(vector<vector<
阅读全文
摘要:无脑深搜两次,一次判断是否位于内部,一次变‘X’ class Solution { public: bool is_inside(int x, int y){ if (x > 0 && x < len_x - 1 && y > 0 && y < len_y - 1) return true; ret
阅读全文
摘要:就开心愉快的dfs即可~ class Solution { public: int get_record(int *record, int x, int y){ return record[x * len_y + y]; } void set_record(int *record, int x, i
阅读全文
摘要:class Solution { public: bool is_cross(string &word1, string &word2){ bool flag = true; for(int i=0;i<word_length;++i) if(word1[i] != word2[i]) if(fla
阅读全文
摘要:class Solution { public: void bfs(vector<vector<int>>& grid){ record[0][0] = 1; p_q.push(make_pair(0,0)); int cur_x, cur_y; int len_x = grid.size(); i
阅读全文