![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
itsRae
这个作者很懒,什么都没留下…
展开
-
leetcode 26
leetcode26class Solution {public:int removeDuplicates(vector& nums) {if(nums.empty())return 0;if(nums.size()==1)return 1;int i=0;int len=1;for(int j=1;j<nums.size();++j){if(nums[i]!...原创 2018-12-28 20:46:23 · 131 阅读 · 0 评论 -
Leetcode 35(二分查找通用)
class Solution {public: int searchInsert(vector<int>& nums, int target) { int low = 0, high = nums.size()-1; // Invariant: the desired index is between [low, high+1] ...转载 2018-12-28 21:15:56 · 151 阅读 · 0 评论 -
leetcode 53.最大自子序和
Dynamic ProgrammingThere is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nu...转载 2018-12-28 21:59:55 · 203 阅读 · 2 评论 -
leetcode39 组合总和
class Solution {public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { return combinationSum(candidates, target, 0); } vector&转载 2019-01-03 20:58:34 · 411 阅读 · 1 评论 -
leetcode 88合并2个有序数组
class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int all = m+n; while(m>0 && n>0) { ...转载 2018-12-29 20:59:45 · 139 阅读 · 0 评论 -
leetcode121 122 123 309买卖股票的最佳时机
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(len <= 1) return 0; int cur_min = prices[0]; int ...转载 2018-12-29 21:36:49 · 159 阅读 · 0 评论 -
leetcode15 三数之和
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; int size = nums.size(); if(size<3)...转载 2018-12-31 21:02:50 · 107 阅读 · 0 评论 -
leetcode 103 二叉树的锯齿形层次遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-01-10 21:27:52 · 162 阅读 · 0 评论 -
leetcode31 下一个排列
class Solution {public: // 8 4 7 6 5 3 1 void nextPermutation(vector<int>& nums) { int len = nums.size(); int i,j; for(i=len-2;i>=0;--i) //找4 {...转载 2019-01-01 16:56:44 · 129 阅读 · 0 评论 -
leetcode 144二叉树的前序遍历
1.递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...原创 2019-01-11 21:06:56 · 250 阅读 · 1 评论 -
leetcode145二叉树的后序遍历
1.递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...转载 2019-01-11 22:07:47 · 198 阅读 · 0 评论 -
leetcode20 有效的括号
参考:https://www.cnblogs.com/thefirstfeeling/p/5916359.html转载 2019-01-07 22:05:40 · 95 阅读 · 0 评论 -
leetcode经验
1.最大的k个元素/第k大的数字 用堆(priority_queue)或者快速排序原创 2019-01-23 20:01:34 · 209 阅读 · 0 评论 -
leetcode347 前k个高频元素
map+堆class Solution {public: vector&amp;lt;int&amp;gt; topKFrequent(vector&amp;lt;int&amp;gt;&amp;amp; nums, int k) { vector&amp;lt;int&amp;gt; res; unordered_map&amp;lt;转载 2019-01-23 21:24:04 · 165 阅读 · 0 评论 -
leetcode173 二叉搜索树迭代器
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...转载 2019-01-18 20:08:08 · 283 阅读 · 0 评论 -
leetcode刷题进度
带括号的是需要编程做的,未带括号表示看了一遍没敲代码数组简单:比较经典的:35,53,121,(1), (26), 27, (35),(53),66,(88),118,119没看,(121),122没看,(167)中等:比较经典的:39,11,(15),16,18没看,(31),33,34,(39),40栈简单:比较经典的:(20),(155),225,232,(496),682...原创 2019-01-18 20:16:43 · 614 阅读 · 0 评论 -
leetcode 225 用队列实现栈
参考https://leetcode.com/problems/implement-stack-using-queues/discuss/62527/A-simple-C%2B%2B-solution转载 2019-01-08 15:10:59 · 88 阅读 · 0 评论 -
leetcode 496 下一个更大元素I
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { stack<int> s; map<int,int> s_map; ...转载 2019-01-08 20:41:14 · 133 阅读 · 0 评论 -
leetcode 71
参考:https://leetcode.com/problems/simplify-path/discuss/25680/C%2B%2B-10-lines-solution转载 2019-01-09 10:33:56 · 109 阅读 · 0 评论 -
leetcode94 二叉树的中序遍历
递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...转载 2019-01-09 11:10:51 · 154 阅读 · 0 评论 -
leetcode 150 逆波兰表达式
class Solution {public: int evalRPN(vector<string>& tokens) { unordered_map<string,function<int(int,int)>> map = { {"+",[](int a,int b){return a...转载 2019-01-14 20:18:15 · 148 阅读 · 0 评论 -
leetcode373查找和最小的k对数字
将可能的组合放入priority_queue中,并取最小的k个元素class Solution {public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { using pr=p...转载 2019-01-25 17:19:02 · 810 阅读 · 0 评论 -
leetcode378有序矩阵中的第k小元素(只看了没敲代码)
class Solution{public: int kthSmallest(vector<vector<int>>& matrix, int k) { int n = matrix.size(); int le = matrix[0][0], ri = matrix[n - 1][n - 1]; int mid = 0; while (le...转载 2019-01-25 20:08:54 · 206 阅读 · 0 评论 -
leetcode451 根据字符出现频率排序
class Solution {public: string frequencySort(string s) { unordered_map&lt;char,int&gt; m; for(char c:s) //map存字母 ++m[c]; using pr=pair&lt;char,int&gt;; ...原创 2019-01-25 21:08:56 · 162 阅读 · 0 评论 -
leetcode455
因为每个孩子最多一块饼干就比较简单,能满足一个孩子就发,不需要考虑多个饼干的叠加,如饼干1,2,3 孩子2,4,此时下面算法是错的int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(),g.end()); sort(s.begin()...转载 2019-01-25 21:47:26 · 178 阅读 · 0 评论 -
leetcode703 数据流中的第k大元素
用优先队列保存k个最大的元素值class KthLargest {public: KthLargest(int k, vector&lt;int&gt; nums) { size = k; for(int num:nums) { pq.push(num); //要先push进来排序后再pop ...转载 2019-01-22 20:21:23 · 274 阅读 · 0 评论 -
leetcode215 数组中的第k个最大元素
1.堆(优先队列)按照升序保存k个最大元素class Solution {public: int findKthLargest(vector<int>& nums, int k) { priority_queue<int,vector<int>,greater<int>> pq; for(int n...原创 2019-01-22 22:16:09 · 127 阅读 · 0 评论 -
leetcode376 摆动序列
class Solution {public: int wiggleMaxLength(vector&lt;int&gt;&amp; nums) { if(nums.size()&lt;2) return nums.size(); int flag=0; //flag为1表示前一段序列是上升的,为-1表示前一段是下降的 int len=1;...转载 2019-02-02 16:07:32 · 150 阅读 · 0 评论 -
leetcode392 判断子序列
1.双指针法class Solution {public: bool isSubsequence(string s, string t) { int i=0,j=0; //两个指针,i为s的指针,j为t的指针 while(i<s.size()&&j<t.size()) { while(t...转载 2019-02-03 10:33:14 · 493 阅读 · 0 评论 -
leetcode56 区间合并
class Solution {public: vector<Interval> merge(vector<Interval>& intervals) { vector<Interval> res; if(intervals.empty()) return res; sort(intervals....转载 2019-02-15 14:28:52 · 115 阅读 · 0 评论 -
leetcode75颜色分类
解法一:class Solution { public: void sortColors(int A[], int n) { int second=n-1, zero=0; //second之后的全是2,zero之前的全是0 for (int i=0; i<=second; i++) { ...转载 2019-02-15 15:37:52 · 324 阅读 · 0 评论 -
leetcode5 最长回文子串(未写代码也没看懂)
参考https://www.cnblogs.com/leavescy/p/5878336.htmlhttps://leetcode.com/problems/longest-palindromic-substring/discuss/147548/Direct-c%2B%2B-DP转载 2019-02-21 14:53:24 · 125 阅读 · 0 评论 -
leetcode62 不同路径(未写代码)
参考:https://leetcode.com/problems/unique-paths/discuss/22954/0ms-5-lines-DP-Solution-in-C%2B%2B-with-Explanations转载 2019-02-21 15:02:49 · 144 阅读 · 0 评论 -
leetcode96 不同的二叉搜索树(未写代码)
递推公式G(n) = G(0) * G(n-1) + G(1) * G(n-2) + … + G(n-1) * G(0) //每一项之和都为n-1,如0+n-1=n-1, 1+n-2=n-1…class Solution {public: int numTrees(int n) { int *G=new int[n+1](); G[0]=1; ...转载 2019-02-21 15:32:15 · 99 阅读 · 0 评论 -
leetcode55跳跃游戏
求每个节点可达的最远位置class Solution {public: bool canJump(vector<int>& nums) { int len=nums.size(); //最终位置坐标为len-1 int max_pos=0; //当前节点可以达到的最大位置 bool res=false; ...转载 2019-01-30 10:30:17 · 272 阅读 · 0 评论 -
leetcode134 加油站
根据要求, 依次以每一个 station 作为起点, 模拟整个过程。比如,A,B,C,D,E,F,G 几个站, 以 B 站为起点, travel 到 F 站时, 汽油不够。 那么我们就可以确定分别以 B, C, D, E 几个站为起点,都到达不了 F 站。 因为既然 B 站能到达 C, D, E 几个站, 那么到达的时候,汽油量一定是大于等于 0 的。这时候再从 C, D, E 出发,汽油量实际...原创 2019-01-30 11:28:29 · 747 阅读 · 0 评论 -
leetcode242 有效的字母异位词
1.排序2.建map,键为字母,值为出现的次数参考:https://leetcode.com/problems/valid-anagram/discuss/66519/2-C%2B%2B-Solutions-with-Explanations转载 2019-02-09 17:38:23 · 100 阅读 · 0 评论 -
leetcode350两个数组的交集II
参考:https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82263/C%2B%2B-hash-table-solution-and-sort-%2B-two-pointers-solution-with-time-and-space-complexity转载 2019-02-11 12:03:53 · 118 阅读 · 0 评论 -
leetcode234回文链表(未写代码)
参考:https://blog.csdn.net/liuxiao214/article/details/77803348转载 2019-02-27 14:34:33 · 90 阅读 · 0 评论 -
leetcode 21合并两个有序链表(未写代码)
参考https://leetcode.com/problems/merge-two-sorted-lists/discuss/9713/A-recursive-solution(递归)https://leetcode.com/problems/merge-two-sorted-lists/discuss/9714/14-line-clean-C%2B%2B-Solution(非递归)...转载 2019-02-27 14:50:43 · 94 阅读 · 0 评论