
LeetCode练习题
Allenlzcoder
拒绝拖延症!!!
展开
-
【重要+细节】LeetCode 149. Max Points on a Line
LeetCode 149. Max Points on a LineSolution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcode-149-max-points-on-a-line/ count by slope Time complexity:O(n2)O(n2)O(n^2) Space complexity:...转载 2018-08-28 21:37:33 · 467 阅读 · 0 评论 -
LeetCode 68. Text Justification
LeetCode 68. Text JustificationSolution1:我的答案 除了写的比较慢,没有其他很大的问题。。class Solution {public: vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<s...原创 2018-08-28 16:31:08 · 277 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion
LeetCode 6. ZigZag ConversionSolution1:我的答案 总是因为各种各样的傻逼错误导致不能AC,脚踏实地很重要! 这个笨方法实在是有点慢。class Solution {public: string convert(string s, int numRows) { string res = ""; if (!s....原创 2018-08-28 14:03:01 · 437 阅读 · 0 评论 -
LeetCode 59. Spiral Matrix II
LeetCode 59. Spiral Matrix IISolution1:我的答案 思路和第54题差不多,就是有点慢。。。。class Solution {public: vector<vector<int>> generateMatrix(int n) { if (n == 1) return {{1}}; vect...原创 2018-08-27 15:28:11 · 848 阅读 · 0 评论 -
LeetCode 54. Spiral Matrix
LeetCode 54. Spiral Matrix循环输出矩阵元素,中等难度题目,查数要认真。 Solution1:我的答案class Solution {public: vector&lt;int&gt; spiralOrder(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) { if (!matrix.size()原创 2018-08-27 15:12:44 · 406 阅读 · 0 评论 -
LeetCode 119. Pascal's Triangle II
LeetCode 118. Pascal’s TriangleSolution1:我的答案 简单题目!!!OK!class Solution {public: vector&lt;int&gt; getRow(int rowIndex) { if (!rowIndex) return {1}; vector&lt;int&gt; res; ...原创 2018-08-27 14:03:58 · 266 阅读 · 0 评论 -
LeetCode 118. Pascal's Triangle
LeetCode 118. Pascal’s TriangleSolution1:我的答案 简单题,一遍过!class Solution {public: vector<vector<int>> generate(int numRows) { if (!numRows) return {}; vector<vecto...原创 2018-08-27 13:55:51 · 254 阅读 · 0 评论 -
LeetCode 29. Divide Two Integers
LeetCode 29. Divide Two IntegersSolution1: 参考网址:[1]https://blog.csdn.net/qq_31617121/article/details/80458587 [2]http://www.cnblogs.com/grandyang/p/4431949.html 相当于复杂度优化到logN. 如 100 3 普通方法是 3...转载 2018-08-27 11:20:41 · 265 阅读 · 0 评论 -
LeetCode 30. Substring with Concatenation of All Words
LeetCode 30. Substring with Concatenation of All WordsSolution1: 转载自:http://www.cnblogs.com/grandyang/p/4521224.html,这道题的思路和LeetCode 567. Permutation in String非常像。滑动窗口的思路要牢记啊!!class Solution {pu...转载 2018-08-25 20:27:15 · 225 阅读 · 0 评论 -
【大数相乘】LeetCode 43. Multiply Strings
LeetCode 43. Multiply StringsSolution1:我的答案 作为一个hard题,提交一次就过真是让我hin开心啊!!! 就是方法有点笨。。class Solution {public: string multiply(string num1, string num2) { if (num1 == &quot;0&quot; || num2 == &quot;0&quot;...原创 2018-08-14 13:44:47 · 403 阅读 · 0 评论 -
【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring
LeetCode 76. Minimum Window Substring参考链接: Solution1:转载 2018-08-12 23:20:32 · 269 阅读 · 0 评论 -
【细节实现题】LeetCode 56. Merge Intervals
LeetCode 56. Merge IntervalsSolution1:我的答案这道题思路不能,有个坑爹的地方在于输入的区间向量未必是有序的,所以利用优先队列priority_queue来排序/** * Definition for an interval. * struct Interval { * int start; * int end; * ...原创 2018-08-12 02:31:39 · 282 阅读 · 0 评论 -
【细节实现题】LeetCode 57. Insert Interval
LeetCode 57. Insert IntervalSolution1:我的答案 算法很简单,不知道为何会被标记为hard题目/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {}...原创 2018-08-12 01:43:30 · 292 阅读 · 0 评论 -
【整数转字符串】LeetCode 9. Palindrome Number
LeetCode 9. Palindrome NumberSolution1: 不利用字符串class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; int res = 0, base = 1, x_copy = x; while ...原创 2018-08-11 13:01:31 · 271 阅读 · 0 评论 -
【翻转整数考虑溢出】LeetCode 7. Reverse Integer
LeetCode 7. Reverse IntegerSolution1:最笨的方法class Solution {public: int reverse(int x) { if (!x) return x; int flag = x &gt; 0? 1: -1; long long res = 0; int tem...原创 2018-08-11 12:18:09 · 321 阅读 · 0 评论 -
【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph
LeetCode 133. Clone GraphSolution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html 真是一道记忆化递归的好题啊~/** * Definition for undirected graph. * struct UndirectedGraphNode { * in...转载 2018-08-10 17:45:28 · 347 阅读 · 0 评论 -
【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break IISolution1:我的答案 纯DFS,在第31个case时超时,还是记录一下。。class Solution { // DFSpublic: vector<string> wordBreak(string s, vector<string>& wordDict) { unor...转载 2018-08-09 23:32:31 · 709 阅读 · 0 评论 -
【记忆化递归+DP】LeetCode 139. Word Break
LeetCode 139. Word BreakSolution1: 记忆化递归的典型套路题 参考网址:https://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/ class Solution { //记忆化递归!public: bool wordBreak(string s, vector<st...转载 2018-08-08 17:52:07 · 846 阅读 · 0 评论 -
【重点!DP】LeetCode 115. Distinct Subsequences
LeetCode 115. Distinct SubsequencesSolution1: 不会做。。 参考网址:https://www.youtube.com/watch?v=mPqqXh8XvWY Time complexity: O(|s|∗|t|)O(|s|∗|t|)O(|s| * |t|) Space complexity: O(|s|∗|t|)O(|s|∗|t|)O(|...转载 2018-08-07 23:14:18 · 215 阅读 · 0 评论 -
【重点!DP】LeetCode 639. Decode Ways II
LeetCode 639. Decode Ways II参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/ Solution1: 动态规划 先写清楚思路,再写代码~// Author: Huahua// Runtime: 58 msclass Solut...转载 2018-08-05 15:10:52 · 773 阅读 · 0 评论 -
【暴力】LeetCode 300. Longest Increasing Subsequence
LeetCode 300. Longest Increasing SubsequenceSolution1:我的答案 暴力搜索,时间复杂度O(n2)O(n2)O(n^2)class Solution {public: int lengthOfLIS(vector&amp;lt;int&amp;gt;&amp;amp; nums) { int n = nums.size(), res =...原创 2018-08-05 01:50:15 · 290 阅读 · 0 评论 -
【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways
LeetCode 91. Decode WaysSolution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了。。。class Solution {public: int numDecodings(string s) { //先来一波DFS int res = 0, start = 0; my_num...转载 2018-08-04 23:27:07 · 772 阅读 · 0 评论 -
【重点!记忆化递归+DP】LeetCode 72. Edit Distance
LeetCode 72. Edit Distance参考链接:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-72-edit-distance/ 思路: Solution1: 递归// Author: Huahua// Runtime: 13 msclass Solution {public: ...转载 2018-08-04 02:15:30 · 530 阅读 · 0 评论 -
【DP】LeetCode 64. Minimum Path Sum
LeetCode 64. Minimum Path SumSolution1:标准的动态规划题目class Solution {public: int minPathSum(vector<vector<int>>& grid) { if (!grid.size() || !grid[0].size()) re...原创 2018-08-03 23:41:10 · 254 阅读 · 0 评论 -
【To Understand !!! DP or 递归】LeetCode 87. Scramble String
LeetCode 87. Scramble String参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http://www.cnblogs.com/grandyang/p/4318500.html Solution1: 递归class Solution {public: bool isSc...转载 2018-08-03 23:23:41 · 273 阅读 · 0 评论 -
【重点!DP】LeetCode 97. Interleaving String
LeetCode 97. Interleaving String参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.cnblogs.com/grandyang/p/4298664.html 【初始化】这道题的大前提是字符串s1和s2的长度和必须等于s3的长度,如果不等于,肯定返回false。当s1和s2是空...转载 2018-08-03 17:46:24 · 279 阅读 · 0 评论 -
【DP】LeetCode 85. Maximal Rectangle
LeetCode 85. Maximal RectangleSolution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk 根据第84题完成!class Solution {public: int maximalRectangle(vector&amp;lt;vector&amp;lt;char&amp;gt;&amp;gt;&amp转载 2018-08-02 11:07:19 · 282 阅读 · 0 评论 -
【DP】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum SubarraySolution1:我的答案 动态规划class Solution {public: int maxSubArray(vector&lt;int&gt;&amp; nums) { int n = nums.size(); if (!nums.size()) return -1; ...原创 2018-08-02 01:13:37 · 315 阅读 · 0 评论 -
【DP】LeetCode 120. Triangle
LeetCode 120. TriangleSolution1:我的答案 真费劲啊!!! 啊!!!!!!class Solution {public: int minimumTotal(vector<vector<int>>& triangle) { if (!triangle.size() || !t...原创 2018-07-28 20:37:33 · 322 阅读 · 0 评论 -
【贪心+双指针】LeetCode 11. Container With Most Water
LeetCode 11. Container With Most WaterSolution1: 参考网址:http://www.cnblogs.com/grandyang/p/4455109.html 定义两个指针向中间走,贪心啊。。class Solution {//public: int maxArea(vector<int>& height) {...转载 2018-07-28 19:09:07 · 403 阅读 · 0 评论 -
【贪心】LeetCode 3. Longest Substring Without Repeating Characters
LeetCode 3. Longest Substring Without Repeating CharactersSolution1:我的答案 该方法中哈希表记录的是字符出现的次数。标准的贪心算法,超过80%的答案; 如果哈希表记录字符出现的位置则还能进一步优化。class Solution {public: int lengthOfLongestSubstring(str...原创 2018-07-28 17:58:38 · 471 阅读 · 0 评论 -
【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee
LeetCode 714. Best Time to Buy and Sell Stock with Transaction FeeSolution1: 参考网址:http://www.cnblogs.com/grandyang/p/7776979.html sold[i]sold[i]sold[i]表示第iii天卖掉股票此时的最大利润,hold[i]hold[i]hold[i]表示第ii...转载 2018-07-26 15:46:13 · 306 阅读 · 0 评论 -
【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
LeetCode 309. Best Time to Buy and Sell Stock with CooldownSolution1: 比较有难度的一道动态规划题了! 参考网址:http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-309-best-time-to-buy-and-sell-stock-with-cool...转载 2018-07-26 14:46:21 · 203 阅读 · 0 评论 -
【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV
LeetCode 188. Best Time to Buy and Sell Stock IVSolution1:我的答案转载 2018-07-25 15:27:51 · 551 阅读 · 0 评论 -
【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III
LeetCode 123. Best Time to Buy and Sell Stock IIISolution1:转载 2018-07-25 02:40:51 · 361 阅读 · 0 评论 -
【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II
LeetCode 122. Best Time to Buy and Sell Stock IISolution1:我的答案 贪心和DP傻傻分不清!class Solution {public: int maxProfit(vector<int>& prices) { int max_pro = 0, n = prices.size(); ...原创 2018-07-25 02:24:24 · 297 阅读 · 0 评论 -
【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock
LeetCode 121. Best Time to Buy and Sell StockSolution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~class Solution {public: int maxProfit(vector<int>& prices) { int n = prices.size(); ...原创 2018-07-25 02:17:50 · 280 阅读 · 0 评论 -
【贪心】LeetCode 55. Jump Game
LeetCode 45. Jump Game II参考网址:http://www.cnblogs.com/grandyang/p/4373533.html Solution1: 贪心算法,这里贪婪并不是要在能跳的范围中选跳力最远的那个位置,因为这样选下来不一定是最优解,这么一说感觉又有点不像贪婪算法了。我们这里贪的是一个能到达的最远范围,我们遍历当前跳跃能到的所有位置,然后根据该位置上的跳...转载 2018-07-24 15:02:53 · 257 阅读 · 0 评论 -
【贪心 和 DP】LeetCode 55. Jump Game
LeetCode 55. Jump Game貌似一直就没做几个贪心的题,是时候好好学习一个了。。 参考网址:http://www.cnblogs.com/grandyang/p/4371526.html Solution1:贪心 时间复杂度O(n)O(n)O(n),空间复杂度O(1)O(1)O(1) 贪婪算法Greedy Algorithm,因为我们并不是很关心每一个位置上的剩余步数,...转载 2018-07-24 11:39:11 · 475 阅读 · 0 评论 -
【分治】LeetCode 69. Sqrt(x)
LeetCode 69. Sqrt(x)参考网址:http://www.cnblogs.com/grandyang/p/4346413.html Solution1:class Solution {public: int mySqrt(int x) { if (x <= 1) return x; int left = 0, right = ...转载 2018-07-24 00:07:08 · 228 阅读 · 0 评论