- 博客(17)
- 收藏
- 关注
原创 [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
原题:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at l
2018-01-13 16:00:22
348
原创 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =)Trapping Rain WaterGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after rai
2018-01-13 15:59:16
234
原创 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言一维动态规划根据转移方程,复杂度一般有两种情况。func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1)func(i) 和 func(1~i-1)有关,时间复杂度是O(n*n),这种情况下空间复杂度一般无法优化,依然为O(n)本篇讨论第一种情况 例题 1Jump GameGiv
2017-12-17 20:46:26
1008
原创 [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes
引言Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧。 例题 1Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by
2017-12-10 23:43:32
255
原创 Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度。原题如下Gas StationThere are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a
2017-11-19 22:44:17
275
原创 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers
数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等。这类题目首先要注意计算过程中本身的特殊情况。比如求相除,则必须首先反映过来除数不能为0。其次要记得考虑负数的情况,如果计算范围不单单是整数,还要考虑double的比较方式。最后要注意越界情况,这个是最容易犯错的,只能具体问题具体分析。例题 1 不用"+
2017-11-05 20:45:11
419
原创 Populating Next Right Pointers in Each Node I, II
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next righ
2017-10-29 09:49:01
224
原创 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果。LeetCode 上面的这三道题最能展现递推思想的美丽之处了。题1 Best Time to Buy and Sell Stock Say you have an array for which the ith element is th
2017-10-29 09:42:29
253
原创 Binary Tree Maximum Path Sum
题目:Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree,
2017-10-29 09:41:49
195
原创 数组的最长连续数, O(n)解法
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3
2017-10-29 09:40:13
340
原创 Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
原题:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at l
2017-10-29 09:38:45
461
原创 链表的交错重排L1,Ln,L2,Ln-1 ....
其实一开始并没有想到时间上O(n)的方法,想到了也是空间复杂度是O(n)的(需要用到栈或者递归):链表分两段,用栈记录第一段的遍历过程。后来经提示想到了,可以将第二段链表逆序。从而不需要额外的辅助空间,在O(n)完成。/** * Definition for singly-linked list. * struct ListNode { * int val; *
2017-10-29 09:37:44
1126
原创 LRU 算法实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if
2017-10-29 09:36:07
274
原创 二叉树系列 - 二叉树的前/中/后序遍历(非递归)
二叉树的遍历是二叉树中最最基础的部分。这里整理二叉树不用递归实现三种顺序遍历的方式。不用递归的话,一般需要栈来完成。当然线索二叉树(不需要栈或递归)也可以完成中序遍历,这里着重讨论使用栈的实现方式。中序遍历(1) 双while,第二个内层while是为了不断压入left child。 vector inorderTraversal(TreeNode *r
2017-10-29 09:27:54
249
原创 大数问题-相加和相乘
Multiply Strings No.43Note:The length of both num1 and num2 is Both num1
2017-09-17 14:20:20
463
原创 二叉树的深度
1.二叉树的最小深度 No.111Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Defini
2017-09-10 14:56:05
187
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人