![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LintCode
Immortal_s
这个作者很懒,什么都没留下…
展开
-
leetcode 42 接雨水
原本想着和LeetCode84柱状图中最大矩形的题是类似的,尝试着用单调栈的方式解,但好像不对。 84那道题用单调栈能解是因为,每次拿出一个坐标下的柱状图长度的时候,我都能立刻知道这个柱状图能不能和右面的柱状图拼接在一起。而这道题还需要左右的矩形协助才能知道能接多少雨水。 换一种说法,用单调栈的时候只能看到池子一边的高度,无法同时看到池子两边的高度,所以无法判断该向其中加多少水。 因为两端高中间低才能放的下雨水,所以假设中间一定有一个高度是最高的。那么从两端开始向中原创 2020-06-09 20:23:11 · 111 阅读 · 0 评论 -
leetcode 88 合并两个有序数组
这道题应该是给你两个有序链表,让你合成一个有序链表的变种。和链表不一样的是,数组中的元素是顺序排列的,如果按照链表的方式处理,可能会出现新值覆盖掉旧值的情况。 所以就会有两种解决方案,第一种是将nums1里的元素复制到新的数组中,然后按照链表的方式处理,空间复杂度为O(m),时间复杂度为O(m+n)。 第二种是从后向前进行遍历,将两个数组中相对大的元素放入nums1的对应位置,空间复杂度是O(1),时间复杂度是O(m+n)。 class Solutio原创 2020-06-07 21:55:57 · 133 阅读 · 0 评论 -
leetcode 53 最大子序和
最开始想的是,我用双指针的方式一前一后进行遍历,如果区间内的数值大于0,就将快指针+1,否则将慢指针+1,直至区间内的值为正。这样写会有一个问题,那就是我默认区间最大值为正值。这种默认是不对的。 所以将算法改为dp,dp 的特点我感觉和递归类似,都是可以将问题分解为相同目标的子问题,将规模缩小,然后再一步步的扩展到正常规模,中间用记忆化数组对其进行加速。 因为题目要求为连续子序列,那么数组中第i个元素就之和第i-1个元素相关。dp公式就很好写。dp[i] = ma原创 2020-06-07 21:54:49 · 140 阅读 · 0 评论 -
记录下leetcode 剑指offer面试题13
记录下leetcode 剑指offer面试题13是一道中等题,提交了4次才过。。。尴尬。。。题目描述如下:地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37]...原创 2020-02-28 11:57:22 · 197 阅读 · 0 评论 -
LintCode-Min Stack
Description Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost.题目模板class MinSt...原创 2018-08-16 20:22:26 · 97 阅读 · 0 评论 -
【LintCode】 -- Subsets II
【LintCode】 – Subsets II和上一道题一脉相承,就是逻辑有点绕。说下思路,也是dfs书,只不过要加一些判断。比如说[1,2,2]这个输入,当处理到第二个2的时候,要判断前面是否添加了第1个2,如果添加了才能把这个2添加进去。如下面这个图。只有前面添加了,才可能添加重复的2,要不然会变成重复元素。推广一下,当添加第n个重复元素的时候,要先判断这个vector...原创 2018-08-22 19:27:01 · 106 阅读 · 0 评论 -
[LintCode] -- Permutations
说两件事第一件 这篇文章是补昨天的。。。第二件 题解换风格。都是看过题和数据并且思索了一番才来百度的,所以根本没必要再放题目和模板上去了,以后直接思路和代码。大概思路这是一个全排序的题,所以可以用DFS深搜树来解决,很简单。代码class Solution {public: /* * @param nums: A list of integers. * @re...原创 2018-08-20 19:27:49 · 728 阅读 · 0 评论 -
[lintCode] -- Permutations II
好,今天的第二题,正好和上一道题是接着的大体思路刚开始以为以为只要nums[i]和nums[n]相等,就不做交换这样做就可以。但是后来想到每次交换完顺序都会变乱,所以这种方法行不通。然后又想了另一种方法,就是每次交换的时候判断在当前顺序中是不是已经交换过,既在i到j-1范围内没有和nums[j]相等的数,那么就交换,否则continueclass Solution {public:...原创 2018-08-20 20:37:42 · 104 阅读 · 0 评论 -
【LintCode】 -- Subsets
【LintCode】 – Subsets大概思路给你一个数组,让你求他的子集,其中难点在于子集中的顺序必须是非降序的。然后这道题的思路参考了上一道题的全排序的思路。先把给的数组进行排序,从小到大。然后用递归,每次取一位,剩下的从本位后面取。当取到指定位数的时候就添加到vector里面。class Solution {public: /** * @param nums: A...原创 2018-08-21 21:04:57 · 152 阅读 · 0 评论 -
LintCode-First Position of Target
First Position of Target Description For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number...原创 2018-08-18 20:57:15 · 121 阅读 · 0 评论 -
LintCode-Kth Largest Element
Description Find K-th largest element in an array. You can swap elements in the array Have you met this question in a real interview? Yes Example In array [9,3,2,4,8], the...原创 2018-08-08 20:18:15 · 276 阅读 · 0 评论 -
LiteCode-Search Range in Binary Search Tree
Description Given a binary search tree and a range [k1, k2], return all elements in the given range. Example If k1 = 10 and k2 = 22, then your function should return [12, 20, 22]. ...原创 2018-08-15 20:38:48 · 1040 阅读 · 0 评论 -
LintCode-Trailing Zeros
2.Trailing Zeros Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 11! = 39916800, so the out should be 2 Challenge O(log ...原创 2018-08-05 20:58:50 · 215 阅读 · 0 评论 -
LintCode-Digit Counts
Digit Counts Description Count the number of k’s between 0 and n. k can be 0 - 9. Have you met this question in a real interview? Yes Example if n = 12, k = 1 in [0, 1, 2...原创 2018-08-06 14:22:01 · 152 阅读 · 0 评论 -
LiteCode-Fizz Buzz(single if)
Description Given number n. Print number from 1 to n. But: when number is divided by 3, print "fizz". when number is divided by 5, print "buzz". when number is divided by both 3 an...原创 2018-08-14 20:02:40 · 254 阅读 · 0 评论 -
LintCode-Serialize and Deserialize Binary Tree
Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct th...原创 2018-08-11 20:58:17 · 170 阅读 · 0 评论 -
LintCode-Rotate String
Description Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given "abcdefg". offset=0 => "abcdefg"offset=1 => "gabcdef"offset=...原创 2018-08-12 10:30:24 · 150 阅读 · 0 评论 -
LintCode-Ugly Number II
Description Ugly number is a number that only have factors 2, 3 and 5. Design an algorithm to find the *n*th ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12... ...原创 2018-08-07 21:25:28 · 137 阅读 · 0 评论 -
LintCode-Implement strStr()
Description For a given source string and a target string, you should output the first index(from 0) of target string in source string. If target does not exist in source, just return -1....原创 2018-08-17 21:26:58 · 150 阅读 · 0 评论 -
LintCode-Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How c...原创 2018-08-09 19:17:26 · 189 阅读 · 0 评论