Leetcode
heheSakura
这个作者很懒,什么都没留下…
展开
-
两数相加(Leetcode1)、两个单项链表相加(Leetcode 2、445)
Leetcode2-Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add th...原创 2019-03-14 16:31:40 · 189 阅读 · 0 评论 -
Leetcode顺时针打印矩阵
Leetcode 54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Outpu...原创 2019-03-14 20:24:25 · 358 阅读 · 0 评论 -
二叉树的各种遍历(前中后非递归、层次、s形遍历)
之前去头条面试,凉在了二面的非递归二叉树遍历上,为避免再凉在同样的问题上,所以总结了一下。 二叉树有前序、中序、后序、层次、s形等等,前三种有用递归实现(下面就不实现了,很简单),非递归实现。其实层次遍历就是广度优先遍历(BFS),深度优先遍历就是前中后序遍历。下面分别来实现一下。 二叉树定义 typedf struct node{ int data; struct node* lchil...原创 2019-03-14 22:53:41 · 1309 阅读 · 1 评论 -
两个字符串的编辑距离 Leetcode72
问题大家都知道,就不赘述了,进入分析。 计算字符串s1,s2的编辑距离,学动态规划都用到的经典例题。用一个二维数组,来记录s1从s1[0]到s1[i]变换到s[0]到s[j]需要的操作数。 dp[i][0]记录了当字符串s2为空时,s1需要进行的步骤数,当然是全部删掉咯,所以dp[i][0]=i,同理dp[0][j]=j; dp[i][j]记录s1[0]~s1[i]到s2[0]~s2[j]的编辑...原创 2019-03-15 10:54:46 · 372 阅读 · 0 评论 -
Fibonacci Number斐波那契数
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1...原创 2019-03-15 13:17:23 · 451 阅读 · 0 评论 -
区间合并leetcode56
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, m...原创 2019-03-17 15:53:19 · 273 阅读 · 0 评论 -
水坑面积
题目 是的,如上,我是一个懒人 输入是一个数组,数组中的元素代表每一个位置的房子的高度,输出积水量。 举例输入 5 7 6 10 7 5 4 5 7 输出 7 思路 遍历数组的每一个元素,找到当前元素左边的最高值,右边的最高值,则改元素能形成的积水量=min(左边最高值,右边最高值)-当前元素值(当然前面的最小值要小于当前元素值)。 但这样直接的方式时间复杂度就是O(n2)O(n...原创 2019-03-15 00:45:41 · 410 阅读 · 0 评论 -
Leetcode Array(27,26)
给定一个数组nums和一个val值,移除数组中所有的val值,且保证最后数组中的元素在正确的位置,并返回这个新数组的长度。 要求不能另外开辟一个新数组,要求空间复杂度是O(1)O(1)O(1)。 元素的位置可以改变,超出新数组长度的部分是什么都没关系。 例子: Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return...原创 2019-04-07 15:40:02 · 389 阅读 · 0 评论