随笔分类 -  LeetCode

摘要:实例n = 3,所有的合法序列((())) (()())(())()()(())()()()思路针对一个长度为2n的合法排列,第1到2n个位置都满足如下规则左括号的个数≥右括号的个数所以,我们就可以按照这个规则去打印括号假设在位置k我们还剩余left个左括号和right个右括号如果left和righ... 阅读全文
posted @ 2014-07-11 21:34 jihite 阅读(5332) 评论(0) 推荐(1) 编辑
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space i... 阅读全文
posted @ 2014-06-24 21:09 jihite 阅读(806) 评论(0) 推荐(0) 编辑
摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number... 阅读全文
posted @ 2014-06-23 19:54 jihite 阅读(328) 评论(0) 推荐(0) 编辑
摘要:已知二叉树的中序加前序或后续可以还原出二叉树(注:中序是必须知道的)前序:a b c中序:b a c后续:b c a1. 前序 + 中序思路对于例图中,由前序可知,第一个元素即a是根节点,从对应的中序中找到a。从而进一步知道其左边的b在左树中,其右边的c在右树中,这样结合前序递归可以还原出整个树。参... 阅读全文
posted @ 2014-06-14 18:37 jihite 阅读(12894) 评论(0) 推荐(1) 编辑
摘要:要求Implement pow(x,n)解1. 特例n=0, 直接返回1n=1, 直接返回xn 0 ? n : -n);pow(x, index) = (index %2==1 ? pow(x, index/2)*x : pow(x, index/2));继续优化pow(x, index) = (i... 阅读全文
posted @ 2014-06-03 17:41 jihite 阅读(810) 评论(0) 推荐(0) 编辑
摘要:要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径。比如下图中,给定路径之和为22,存在路径,因此返回true;否则返回false. 5 / \ 4 8 / / \ 11... 阅读全文
posted @ 2014-05-05 21:33 jihite 阅读(643) 评论(0) 推荐(0) 编辑
摘要:要求给定两个整数(n,k),返回长度为k,从1到n的所有组合(注意1.组合没有顺序 2. [2,3]和[3,2]为同一组,取前者小于后者的那一组)。例如(4,2),结果为:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]思路递归参考代码#inclu... 阅读全文
posted @ 2014-05-04 17:29 jihite 阅读(3003) 评论(0) 推荐(0) 编辑
摘要:要求 给定一没有重复元素的旋转数组(它对应的原数组是有序的),求给定元素在旋转数组内的下标(不存在的返回-1)。例如有序数组为{0,1,2,4,5,6,7},它的一个旋转数组为{4,5,6,7,0,1,2}。元素6在旋转数组内,返回2元素3不在旋转数组内,返回-1分析 遍历一遍,可以轻松搞定... 阅读全文
posted @ 2014-04-24 22:49 jihite 阅读(3701) 评论(2) 推荐(1) 编辑
摘要:题目大意给定一个整形数组,求出最长的连续序列。例如数组[100,4,200,1,3,2],最长的连续序列长度为[1,2,3,4],长度为4。要求时间复杂度为O(n)。思路"排序转换成经典的动态规划问题"的话排序至少需要时间复杂度为O(nlog(n))——pass利用c++中的set,直接会排序,并且... 阅读全文
posted @ 2014-04-19 22:54 jihite 阅读(4279) 评论(0) 推荐(0) 编辑
摘要:分析根结点固定时平衡二叉树个数=左孩子的个数 * 右孩子的个数。又左孩子或右孩子为空是不妨置为1,这样0个结点时,f(0) = 11个结点时,f(1) = f(0) * f(0) = 12个结点时,f(2) = f(0) * f(1) + f(1) * f(0)3个结点时,f(3) = f(0) *... 阅读全文
posted @ 2014-04-08 21:24 jihite 阅读(299) 评论(0) 推荐(0) 编辑
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of ... 阅读全文
posted @ 2014-04-06 22:28 jihite 阅读(440) 评论(0) 推荐(0) 编辑
摘要:题目大意给出一个单链表,和一个K值,根据K值往右旋转,例如:思路先求出链表的长度size,其实按着倒数第K%size个位置旋转,这个位置即size-(K%size)参考代码/** * Definition for singly-linked list. * struct ListNode { * ... 阅读全文
posted @ 2014-04-05 22:40 jihite 阅读(1175) 评论(0) 推荐(0) 编辑
摘要:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路两个指针。一个每次走一步,另一个每次走两步,如果有环,会相遇。/** * Def... 阅读全文
posted @ 2014-04-04 22:26 jihite 阅读(282) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示