05 2014 档案
摘要:原地归并。下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A...
阅读全文
摘要:这道题就是找规律啊!!!想想啊,11和10是可以连续的,那么10和11也是可以连续的。下面是AC代码: 1 /** 2 * The gray code is a binary numeral system where two successive values differ in on...
阅读全文
摘要:这道题用动规啊!!但是呢,有很多细节需要注意的啊!!特别是当0出现的时候。下面就来详细讲下啦!首先设一个长度为len+1的数组num,num[i]表示s[0...i-1]的解码方式。然后我们假设i之前的num都计算好了,现在来计算num[i],考虑s[i-1],s[i-1]一般来说有两种解码方式,一...
阅读全文
摘要:这道题的做法,一定得掌握啊!!! elegant & beautiful & concise下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道...
阅读全文
摘要:这道题采用穷举法。 1 /** 2 * Given a string containing only digits, 3 * restore it by returning all possible valid IP address combinations. 4 ...
阅读全文
摘要:第一道题是Catalan数,主需要求f(n),第二道题是用递归的方法,不断的组装这个棵树。下面是两个AC代码: 1 /** 2 * Given n, how many structurally unique BST's (binary search trees) that store va...
阅读全文
摘要:这道题用DP来解决,也可以用递归,但是时间复杂度很高,被报TLE。下面是DP的AC代码。 1 /** 2 * Solution 2 :DP Accepted 3 * create a (len1+1) x (len2+1) matrix A, A[i][j] means s3[0...
阅读全文
摘要:这道题要求空间复杂度为O(1),则只能采用Morris Traversal进行中序遍历!!这个了解了之后,难点在于如何定位到两个被交换了的节点?我就被困在这里几个小时!!!(允许我为自己的愚蠢表示下悲伤吧!!!)参考了discuss中前辈的算法,才发现很简单!!!我们只需要这样来看问题,BST的中序...
阅读全文
摘要:验证二叉树是否是查找树,可以通过查看它的中序遍历是否是升序的。下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is a valid binary search tree (BST). 3 * solution : 4...
阅读全文
摘要:这两道题,大同小异。 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同。下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...
阅读全文
摘要:不断递归的实现!!!!下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param...
阅读全文
摘要:BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!!这里有个重要的信息:可以用null来标识一个level的结束!!!下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level ord...
阅读全文
摘要:这个题目我觉得是BFS的扩展,我只是对偶数层的节点先放到一个栈中,这样从栈出来后就换了一个顺序。下面是AC代码: 1 /** 2 *Given a binary tree, return the zigzag level order traversal of its nodes' values. ...
阅读全文
摘要:Array和List的区别在于前者可以随机访问,而后者只能顺序访问。对于把排好序的array转成BST,可以用top-down的方式,很直观也很自然,时间复杂度是O(n)。而对于List如果采用同样的方式,每次需要顺序遍历到中间节点,时间复杂度变成O(nlogn),如果换一种思路,down-top,...
阅读全文
摘要:判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的。下面是AC代码: 1 /** 2 * Given a binary...
阅读全文
摘要:这两道题用递归的解法都很简单,只是稍有不同。下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along th...
阅读全文
摘要:二叉树最经典的解法就是采用递归方法,这道题也不例外。下面是AC代码: 1 /** 2 * Given a binary tree and a sum, determine if the tree has 3 * a root-to-leaf path such that ad...
阅读全文
摘要:这题的提示中给出,linked list是原来二叉树的先序遍历。所以我用一个栈暂时存储每个节点的右子树(如果它有右子树的话),然后把左子树交换到右子树,左节点置为null,之后迭代下去,当遇到叶子节点时,从栈中pop出一个子树,再继续,直到遇到叶子节点且栈中为空。下面是AC代码: 1 /** 2 ...
阅读全文
摘要:这道题采用动态规划,可是我一开始没有想到。后来参考了discuss中前辈的代码和思路,才想通的。 方法二是因为每一步只和上一步的内容相关,所以可以只用O(n)的空间复杂度。下面是AC代码: 1 /** 2 * Solution DP 3 * we keep a m*n matri...
阅读全文
摘要:我觉得这两道题没有技巧可言,唯有把逻辑弄清楚,再用代码把自己的逻辑写下来。下面是这两道题的AC代码: 1 /** 2 * Populate each next pointer to point to its next right node. If there is no next right...
阅读全文
摘要:这道题,采用动态规划算法。from top to down,把到每个节点的最小路径和都求出来。下面是AC代码: 1 /** 2 * Given a triangle, find the minimum path sum from top to bottom. 3 * Each ...
阅读全文
摘要:采用2项公式解这问题挺简单,唯一需要注意的是当两个比较大的数相乘时,容易越界,我采用分子、分母同时除以他们的最大公约数。下面是AC代码: 1 /** 2 * Given an index k, return the kth row of the Pascal's triangle. 3 ...
阅读全文
摘要:这道题是生成杨辉三角,看了下杨辉三角的性质,就可以解决了。下面是AC代码: 1 /** 2 * Given numRows, generate the first numRows of Pascal's triangle. 3 * @param numRows 4 * ...
阅读全文
摘要:这道题在前两个的基础上做稍微改进就可以。下面是AC代码: 1 /** 2 * Design an algorithm to find the maximum profit. You may complete at most two transactions. 3 * @pa...
阅读全文
摘要:这道题我一开始想到用递归方法,可以把规模大的问题变成规模小的问题,但是觉得递归的时间复杂度很高,因为它会把相同的问题进行重复计算,然后我想是不是有什么down-up的方法,先把所有的子问题的结果保存起来,但是发现问题的最优解并不能由子问题的最优解推导出来。最后就想到买股票的时候,我们在一个局部极小的...
阅读全文