
每日一题
ChasingTheFreeWind
这个作者很懒,什么都没留下…
展开
-
leetcode每日一题 990. 等式方程的可满足性(并查集)
class Solution {public: map<int,int> father; int Find(int x) { int r = x; while(father[r]!=r) r = father[r]; int i = x; int j; while(father[i]!=i) { j = father[i];.原创 2020-06-08 11:21:20 · 144 阅读 · 0 评论 -
leetcode每日一题 128.最长连续序列(并查集)
并查集思路并查集,遍历数组,如果该数字+1在数组中则将它们合并。代码class Solution { unordered_map<int,int> father,cnt; int Find(int x) { int r = x; while(r!=father[r]) { r = father[r]; } int i = x; int j; .原创 2020-06-06 12:18:00 · 163 阅读 · 0 评论 -
leetcode每日一题 面试题29. 顺时针打印矩阵
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> ans; int N = matrix.size(); if(N==0) return ans; int M = matrix[0].size(); int .原创 2020-06-05 18:12:44 · 139 阅读 · 0 评论 -
leetcode每日一题 136.只出现一次的数字
异或操作:任何数异或0得0,任何数异或自己得0。根据这个性质就可在时间复杂度O(N),空间复杂度O(1)之下求到答案。class Solution {public: int singleNumber(vector<int>& nums) { int single = 0; for(int num:nums) single^=num; return single; }};...原创 2020-05-14 21:04:18 · 262 阅读 · 0 评论 -
leetcode每日一题 50.pow(x,n)
class Solution {public: double quickPow(double x, long n) { double ans = 1.0; double mul = x; while(n) { if(n&1) ans*=mul; mul*=mul; n/=2; } r.原创 2020-05-11 18:50:00 · 178 阅读 · 0 评论 -
leetcode每日一题 572.另一个树的子树
思路递归的方法。如果节点相等则判断子树是否相等,如果相等则返回true,反之返回false。如果节点不等,则查找其左子树和右子树,直到找到相等的节点。代码class Solution {public: bool isSame(TreeNode* s, TreeNode* t) { return (s==t)||((s!=NULL&&t!=...原创 2020-05-07 10:35:35 · 218 阅读 · 0 评论 -
leetcode每日一题983. 最低票价(动态规划)
思路不是很难的动态规划,但是最开始一些边界条件没考虑到,WA了好几次,然后面向测试用例debug。设dp[i]为前i天(含)旅游花费的最小金额。dp[i] = min{dp[i-1] + costs[0], dp[i-7] + costs[1], dp[i-30]+costs[2]}然后两个旅游日之间的日子旅游最小花费就等于前一个旅游日的最小花费。即dp[j] = dp[days[i]]...原创 2020-05-06 12:37:49 · 289 阅读 · 0 评论 -
leetcode每日一题-无重复的最长子串(双指针)
官方题解代码class Solution {public: int lengthOfLongestSubstring(string s) { int N = s.length(); unordered_set<char> occ; int rk = -1; int ans = 0; for...原创 2020-05-02 11:31:25 · 327 阅读 · 0 评论 -
leetcode每日一题 1095.山脉数组中查找目标值(三次二分)
三次二分解决问题,注意细节就好。代码/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public: * int get(...原创 2020-04-29 15:28:31 · 163 阅读 · 0 评论 -
leetcode每日一题-46.全排列(库函数&dfs回溯)
简单题可以直接用next_permutation()库函数,也可以使用dfs回溯找到所有结果。方法一 库函数class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int> > a...原创 2020-04-25 11:24:54 · 143 阅读 · 0 评论 -
leetcode每日一题-面试题51. 数组中的逆序对
经典题目使用分治法即可class Solution {public: int merge_count(vector<int>& a) { int N = a.size(); if(N<=1) return 0; int cnt = 0; vector<int...原创 2020-04-24 18:34:32 · 156 阅读 · 0 评论 -
leetcode每日一题-面试题08-11 硬币
参考链接是个动态规划const int MOD = 1e9+7;class Solution {public: int waysToChange(int n) { vector<long long> dp; int val[4] = {25, 10, 5, 1}; dp.resize(n+1); ...原创 2020-04-23 21:21:49 · 212 阅读 · 0 评论