随笔分类 - LeetCode题解
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 class Solution 2 { 3 public: 4 ListNode* mergeKLists(vector<ListNode*>& lists) 5 { 6 vector<int> nums; 7 for(auto a : lists) 8 { 9 ListNode* head =
阅读全文
摘要:1 class Solution 2 { 3 public: 4 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) 5 { 6 int m = nums1.size(),n = nums2.size(); 7
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<int> findSubstring(string s, vector<string>& words) 5 { 6 if(s.empty() || words.empty()) return {}; 7 int n =
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int slidingPuzzle(vector<vector<int>>& board) 5 { 6 int m = 2, n = 3; 7 string start = ""; 8 string target = "123450"
阅读全文
摘要:1 class UnionFind 2 { 3 public: 4 vector<int> parent; // 存储若干棵树 5 vector<int> size; // 记录每棵树包含的节点数 6 7 UnionFind(int n) 8 { 9 parent = vector<int>(n);
阅读全文
摘要:1 //dp[i][j] = x表示,戳破气球i和气球j之间(开区间,不包括i和j)的所有气球,可以获得的最高分数为x 2 //根据刚才对dp数组的定义,如果最后一个戳破气球k,dp[i][j]的值应该为: 3 // dp[i][j] = dp[i][k] + dp[k][j] 4 // + poi
阅读全文
摘要:1 // 1、当s[i] == p[j]时,dp[i][j] = dp[i - 1][j - 1] 2 // 2、当p[j] == '.'时,dp[i][j] = dp[i - 1][j - 1] 3 // 3、当p[j] == '*'时, 4 // 3.1、当s[i] != p[j - 1]时,d
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int longestPalindromeSubseq(string s) 5 { 6 int n = s.size(); 7 // dp 数组全部初始化为 0 8 // 在子串s[i..j]中,最长回文子序列的长度为dp[i][j]
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool canIWin(int M, int T) 5 { 6 if (M*(M+1)/2 < T) return false; 7 if (T <= M) return true; 8 m_ = vector<int>(1 <<
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int eraseOverlapIntervals(vector<vector<int>>& intervals) 5 { 6 if(intervals.empty() || intervals[0].empty()) return
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int maxProfit(vector<int>& prices, int fee) 5 { 6 int n = prices.size(); 7 vector<vector<int>> dp(n,vector<int>(2,0))
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int maxProfit(vector<int>& prices) 5 { 6 if(prices.size() < 2) return 0; 7 int n = prices.size(); 8 vector<vector<int
阅读全文
摘要:1 // 一次交易由买入和卖出构成,至少需要两天。所以说有效的限制 k 应该不超过 n/2,如果超过,就没有约束作用了,相当于 k = +infinity。 2 class Solution 3 { 4 public: 5 int maxProfit(int K, vector<int>& pric
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int maxProfit(vector<int>& prices) 5 { 6 if(prices.empty()) return 0; 7 int n = prices.size(); 8 vector<vector<vector
阅读全文
摘要:1 class Trie 2 { 3 public: 4 bool is_end; //是否以该单词结尾 5 Trie* son[26]; //该节点儿子的个数 6 Trie() 7 { 8 is_end = false; 9 for(int i = 0;i < 26;i ++) son[i] =
阅读全文
摘要:1 class Solution 2 { 3 // 状态 dp[i][j] : 表示 s 的前 i 个字符和 p 的前 j 个字符是否匹配 (true 的话表示匹配) 4 // 状态转移方程: 5 // 1. 当 s[i] == p[j],或者 p[j] == ? 那么 dp[i][j] = dp[
阅读全文
摘要:1 //f(n) 表示从左到右(forward)的最终结果 2 //b(n) 表示从右到左(backward)的最终结果 3 class Solution 4 { 5 public: 6 int leftToRight(int n) 7 { 8 if (n == 1) return 1; 9 els
阅读全文
摘要:1 class Solution 2 { 3 public: 4 double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int K) 5 { 6 vector<pair<double,int>> vec; 7 for
阅读全文

浙公网安备 33010602011771号