leetcode
文章平均质量分 85
李白-2017
今天经历的所有惊心动魄,都不过是明天的过眼云烟。
展开
-
41. First Missing Positive 寻找第一个缺失的正数
例子1:Input: [1,2,0]Output: 3例子2:Input: [3,4,-1,1]Output: 2例子3:Input: [7,8,9,11,12]Output: 1思路:首次遍历,对元素值为(0,n)区间内的元素nums[i],不在对应下标nums[i] - 1位置的进行交换顺序。第二次遍历,判断每个元素nums[i] 是否等于i+1. 找到...原创 2019-09-06 17:02:29 · 180 阅读 · 0 评论 -
380. Insert Delete GetRandom O(1)
设计一个数据结构,插入,删除,随机获取其中一个数值操作都是O(1)的时间复杂度。思考:首先从基本的数据结构开始思考,数组、链表、hash表,map,set等等,发现单一的数据结构是无法支持那些操作都是O(1)的时间复杂度。进而再考虑叠加数据结构的方法。vector + unordered_map 是 不错的叠加方法,用数组存放具体数据,用hash表存放数据的索引。之后再查找时,可以通过索引,再...原创 2019-05-25 19:43:53 · 186 阅读 · 0 评论 -
11. Container With Most Water 最多存水量
求最多能够存水量,(长*高)Example:Input: [1,8,6,2,5,4,8,3,7]Output: 49思考: 这道题有两种解法,方法1,暴力求解,时间复杂度O(n^2),计算出每个可能的面积取最大值。方法2,两个指针,一个left,一个right指针,计算面积, 每次比较这两个指针,低的一边移动指针,这种方法只需要遍历一遍数组。不过不太好想到。解法:(方法2)...原创 2019-05-25 18:46:21 · 216 阅读 · 0 评论 -
289. Game of Life
按照 规则 修改二维数组元素值。二维数组中只有1和0, 1表示live, 2表示dead.规则如下:1. 一个存活的cell,它有小于两个邻居dead,那么它在下一代die.2. 一个存活的cell,它有2到3个邻居live,那么它在下一代也live.3. 一个存活的cell,它有多于3邻居die,那么它在下一代也die.4. 一个dead的cell,它有3个邻居live,...原创 2019-05-25 18:28:13 · 135 阅读 · 0 评论 -
62. Unique Paths 计算从二维数组(0,0)->(m-1,n-1)路径数量
计算从二维数组 (0,0)->(m-1,n-1) 路径数量思考:最初考虑使用dfs解决,这题不适合使用dfs, 而且会超时。正确解法使用动态规划解决。看到下面的递推公式也就明白了。方法1:使用二维的数组存放当前的路径数量。 int uniquePaths(int m, int n) { vector<vector<int>> pat...原创 2019-05-25 17:09:19 · 322 阅读 · 0 评论 -
287. Find the Duplicate Number 寻找一个重复的元素
数组n+1个元素, 元素值范围1到n,其中有一个重复的元素,寻找重复元素。要求:1.不能修改原数组。2.空间复杂度O(1)。3.时间复杂度小于O(n^2)。4.数组中有只有一个重复的元素,但可能重复多次。思考:如果没有哪些要求解题就比较简单了, 方法也多样,例如(方法1)对原数组进行排序,比较当前元素和前一个元素是否相同。(方法2)借助hash表set,遍历元素,如果se...原创 2019-05-25 16:49:48 · 151 阅读 · 0 评论 -
79. Word Search 在二维矩阵中寻找某个word是否出现
例如:board =[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E']]Given word = "ABCCED", return true.Given word = "SEE", return true.Given word = "ABCB", return false.难度:【medium】思...原创 2019-05-19 23:26:09 · 162 阅读 · 0 评论 -
56. Merge Intervals 合并重叠的间隔
对一个序列,将重叠的间隔进行合并。例如:[[1,4],[2,3]] => [[1,4]][[1,3],[2,6],[8,10],[15,18]] => [[1,6],[8,10],[15,18]]难度:【medium】思路:这道题不是很难,只要把各种case考虑到就基本可以写对。首先对序列进行排序,这里序列题目中使用的是vector<vector<...原创 2019-05-19 17:56:49 · 182 阅读 · 0 评论 -
73. Set Matrix Zeroes
当矩阵中有元素是0时,就将它所在的那一行和那一列全都置为0.例如:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]难度:【medium】思考:最初想到的在遍历矩阵每个元素时,如果该元素是0,那么就将它所在的行和列的元素都改为0,可是再一想,发现再遍历下面...原创 2019-05-19 16:08:12 · 110 阅读 · 0 评论 -
162. Find Peak Element 寻找峰值元素
从数组中找到一个峰值,对于n个元素的数组,认为nums[-1] = nums[n] = -∞。峰值的含义:和它相邻的元素相比,比它左右元素值都大,就是峰值,返回峰值的下标。数组中可能有多个峰值,找到一个返回即可。例如:Input: nums = [1,2,3,1]Output: 2 (下标为2的元素是一个峰值)难度:【medium】思路:(方法1)最先想到的方法就是暴力方法。这...原创 2019-05-18 22:01:22 · 561 阅读 · 0 评论 -
78. Subsets 求数组的子数组
难度:【medium】for ex. 输出的子数组的顺序没有限制。Input: nums = [1,2,3]Output:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]主要由两种方法:方法1:深度优先搜索 vector<vector<int>> subs...原创 2019-04-07 17:07:51 · 515 阅读 · 0 评论 -
75. Sort Colors 对三种颜色进行排序
0, 1, 2 分别表示三种颜色。要求对它们进行排序。Example:Input: [2,0,2,1,1,0]Output: [0,0,1,1,2,2]要求:使用 a pass 算法,而不是two-pass算法。这里指的两步算法是,先统计出来0,1,2的count,再按照各自的数量放到数组中。思考:没看要求时,想到的方法也是要求中提到的那种two-pass算法,再思考一步的策略...原创 2019-05-25 21:09:31 · 684 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
已知前序序列和中序序列,构建二叉树。解法:递归 TreeNode *build(vector<int>& preorder, int preStart, int preEnd, vector<int> &inorder, int inStart, int inEnd) { if...原创 2019-05-25 23:57:10 · 93 阅读 · 0 评论 -
73. Set Matrix Zeroes
如果某个元素是0,那么将它所在的一行和一列都改为0.思路:注意,如果在原地直接修改, 不能影响后面的判断。方法有三种:方法1:使用辅助空间记录需要改为0的行和列,空间复杂度O(m+n), 时间复杂度O(m*n)void setZeroes(vector<vector<int>>& matrix) { std::set<int&...原创 2019-05-26 00:26:38 · 115 阅读 · 0 评论 -
84. Largest Rectangle in Histogram 寻找最大的矩形
题意:寻找面积最大的矩形。思路:此题和第42题求解总共存水量的题目都可以使用栈的方法解决。(1)遍历height, 当要插入元素比栈顶元素大时,向栈顶插入元素;当要插入元素比栈顶元素小时,取栈顶元素到当前元素计算面积。(2)先向heights的结尾放入下标0,为了能够驱动当每个矩阵都是升序的时候,从后向前计算每个能够组成的矩形的面积,不断寻找最大的面积,首先,先会计算最后一个矩形的...原创 2019-09-06 16:06:26 · 289 阅读 · 0 评论 -
128. Longest Consecutive Sequence 寻找最长连续子数组
在一个乱序数组中寻找最长的连续子数组。注意:有重复元素时,只计数一次。例子1:Input: [100, 4, 200, 1, 3, 2]Output: 4Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.例子2:Input: ...原创 2019-09-06 14:23:55 · 224 阅读 · 0 评论 -
42. Trapping Rain Water 计算储水量
给定一个数组, 表示高度,计算存水量。例子:Input: [0,1,0,2,1,0,1,3,2,1,2,1]Output: 6方法1:动态规划int trap(vector<int>& height) { if (height.empty()) { return 0; } int n = height.size(); int lef...原创 2019-09-06 11:51:12 · 272 阅读 · 0 评论 -
48. Rotate Image 二维数组向右旋转90度
难度:【medium】对 n * n 的二维数组向右旋转90度。例如:思路:(方法1) 最简单也是最容易想到的解法,就是开辟一个和二维数组相同大小的临时空间,之后将第0行放到临时二维数组的第n-1列,第1行放到临时二维数组的第n-2列,依次类推。使用这种方法在leetcode中也能够通过,但是需要额外的空间。(方法2)这个是discuss中看到的解法,先按行翻转,再按照左对角线...原创 2019-05-18 16:14:17 · 539 阅读 · 0 评论 -
54. Spiral Matrix 螺旋矩阵
将一个二维矩阵,螺旋方式遍历。Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example 2:Input:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12]]Output: [1,2,3,4,...原创 2019-05-26 18:10:06 · 129 阅读 · 0 评论 -
15. 3Sum 找到数组中三数之和为给定值的组合
找到数组中三数之和为给定值的组合。Given array nums = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]思考:求解三数之和,会先联想到求解两数之和。先来回忆一下两数之和怎么求解。方法1,使用unordered_map存放数组值和下标,再遍历数组从寻找是否又差值,有就找到一个...原创 2019-05-26 17:30:05 · 1477 阅读 · 0 评论 -
152. Maximum Product Subarray 最大乘积子数组
给定一个数组,获取数组中子数组的最大乘积。思考:最初想到的就是暴力求解方法,时间复杂度O(n^2)枚举出所有解,最终得到最大乘积。看了discuss中发现有更好的解法。动态规划,用两个变量当前的极大值和极小值,因为后面乘以一个负数的话,极大值会变成极小值,而再乘以一个负数又会变成极大值。所以中间过程中只需要记录这两个值即可。解法: int maxProduct(vector&l...原创 2019-05-26 17:14:11 · 125 阅读 · 0 评论 -
55. Jump Game 是否能跳到最后
数组中每个元素表示从当前位置能够跳的步数,判断是否能跳到最后。思考:本题在solution中有4中解法,(方法1)dfs,实际运行会超时,因为会有数组很长的case. (方法2)dfs优化版,不过也会超时。(方法3)动态规划。(方法4)贪心算法。解法:(方法1)dfs, 虽然会超时,不过这种解法需要掌握。 bool canJumpFromPos1(int pos, vect...原创 2019-05-26 14:58:25 · 181 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
在排序的旋转数组中查找一个值。ex. [4,5,6,7,8,0,1] 是排序的数组,以某一个位置做了一次旋转后的结果。 target 8. 进行查找,返回下标 4.思考:使用二分查找的变种,class Solution {public: int search(vector<int>& nums, int target) { int beg...原创 2019-05-26 13:09:26 · 115 阅读 · 0 评论 -
34. Find First and Last Position of Element in Sorted Array
对排序的数组查找目标值的左右边界。ex. [5,7,7,8,8,10] , target 8 => [3,4].思考:对于已经排序的数组,通常会想到二分插入,这道题使用二分查找的变体即可。分别找到左右边界。 int LeftBound(vector<int> &nums, int target) { int begin = 0;...原创 2019-05-26 12:31:05 · 111 阅读 · 0 评论 -
238. Product of Array Except Self
除了当前元素之外的元素相乘。要求:不能用除法难度:【medium】for ex.Input: [1,2,3,4]Output: [24,12,8,6]// 24 = 2*3*4// 12 = 1*3*4// 8 = 1*2*4// 6 = 1*2*3class Solution {public: vector<int> productEx...原创 2019-04-07 16:48:51 · 132 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II 股票最佳买入和卖出时间
允许股票买入和卖出多次,并且要求只能在卖出后,才能买入。这道题和121. Best Time to Buy and Sell Stock 股票最佳买入和卖出时间不同,121这道题相对比较简单,只要求,买卖一次。而这道题可以买卖多次,得到最大的收益。例如:Input: [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price =...原创 2019-03-31 18:24:18 · 217 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array 对排序的数组删除重复的元素
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-03-30 17:13:53 · 225 阅读 · 0 评论 -
leetcode shell 4道题
最近在学习shell, 发现leetcode上shell的4道题还是比较不错的,整理了一下193. Valid Phone Numbers 电话号码匹配 easygrep '^([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$\|^[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0...原创 2019-02-05 22:55:37 · 205 阅读 · 0 评论 -
leetcode--链表总结
目录1. 两个链表相加2. 链表翻转(逆序)3. 旋转链表4. 链表去重5. 链表合并6. 链表划分7. 移除指定节点8. 运用快慢指针9. 查找两个链表的公共节点10. 与其他数据结构和算法结合11. 深度拷贝复杂链表1. 两个链表相加(1)模拟两个数相加(445. Add Two Numbers II,Medium)思路:两个...原创 2017-10-21 12:29:53 · 341 阅读 · 0 评论 -
33. Search in Rotated Sorted Array 在旋转的排序数组中查找元素
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If foun原创 2018-02-03 22:22:09 · 186 阅读 · 0 评论 -
449. Serialize and Deserialize BST
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be原创 2018-02-03 21:55:16 · 538 阅读 · 0 评论 -
315. Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].Example原创 2018-02-03 21:31:30 · 127 阅读 · 0 评论 -
34. Search for a Range 查找一个区间
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the原创 2018-01-21 23:58:00 · 166 阅读 · 0 评论 -
35. Search Insert Position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2018-01-21 16:46:39 · 155 阅读 · 0 评论 -
leetcode 树总结
下面对leetcode中tree类下面的一些题目进行了总结,其中包括top 100 liked qusetions和top interviewquestions. 1. 二叉树的基本操作find,insert(递归、非递归方法), create, delete.删除的节点带有左右孩子,可以用直接前驱(被删除节点最右分支的节点)或是直接后继(被删除节点最左分支的节点)代替被删除节点原创 2017-12-03 23:59:39 · 371 阅读 · 0 评论 -
leetcode -- 栈总结
栈的相关题目也比较多,这里主要对栈中的easy,top 100 liked qusetions和top interviewquestions做了详细总结。目录1.栈实现队列2.队列实现栈3.包含min函数的栈4.计算算式5.给定序列判断出栈顺序是否可行7. 括号匹配8.与二叉树结合,遍历二叉树9. 求一个区域的题目10. 嵌套迭代器...原创 2017-11-04 21:44:14 · 788 阅读 · 0 评论 -
27. Remove Element 删除数组中指定值的元素
难度:【easy】class Solution {public: int removeElement(vector<int>& nums, int val) { int i = 0; for (int j = 0; j < nums.size(); ++j) { if (nums[j] != val)...原创 2019-03-30 17:49:41 · 463 阅读 · 0 评论 -
53. Maximum Subarray 寻找sum最大的子数组
Given an integer arraynums, find the contiguous subarray(containing at least one number) which has the largest sum and return its sum.例子:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation: [...原创 2019-03-30 18:43:30 · 277 阅读 · 0 评论 -
66. Plus One 把数组看成一个数值进行加1
例如:Input: [1,2,3]Output: [1,2,4]Explanation: The array represents the integer 123.难度:【easy】把[1,2,3]看着数值123,对它加1,结果就是124.需要考虑进位情况。class Solution {public: vector<int> plusOne(ve...原创 2019-03-30 21:31:54 · 166 阅读 · 0 评论 -
268. Missing Number 查找缺少的元素
数组中的元素正常是0,1,2,n。 数组中元素有n-1个元素,查找缺少的那个元素。例如:Input: [9,6,4,2,3,5,7,0,1]Output: 8 (数组中缺少的元素是8)难度:【easy】这道题我想出了三种方法:第一种方法:对原数组进行排序,之后对比下标和该下标对应的元素值,如果不同,那么下标就是缺少的元素。class Solution {public...原创 2019-03-31 17:49:41 · 148 阅读 · 0 评论