摘要: Here is adifficulty and frequency distribution chartfor each problem (which I got from the Internet and is very useful).Dynamic ProgrammingEdit DistanceMaximum SubarrayMinimum Path SumUnique PathsUnique Paths IILongest Palindromic SubstringInterleaving StringTriangleDistinct SubsequencesDecode WaysP 阅读全文
posted @ 2013-08-19 23:32 feiling 阅读(2253) 评论(0) 推荐(0) 编辑
摘要: Implementint sqrt(int x).Compute and return the square root ofx.[解题思路]使用二分法来求解开方,在一个区间中,每次拿中间数的平方来试验,如果小了,再拿右区间的中间数来试。比如求解sqrt(16), (0+16) / 2 = 8, 8*8 = 64 > 16, 选择左区间(0+7)/2 = 3, 3*3 = 9 16继续选择左区间(4+4)/2 = 4, 4*4=16这里为了防止x输入过大时,mid*mid会溢出,把mid*mid与x的比较换成mid与x/mid之间的比较 1 public int sqrt(int x) { 阅读全文
posted @ 2013-08-19 23:00 feiling 阅读(345) 评论(0) 推荐(0) 编辑
摘要: You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?large data TLE递归解法的时间复杂度为:O(2^lgn), 故当n逐渐变大时,由于计算重复解,时间复杂度呈指数级增长 1 public int climbStairs(int n) { 2 // Start typing your Java sol... 阅读全文
posted @ 2013-08-19 21:21 feiling 阅读(400) 评论(0) 推荐(0) 编辑
摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文
posted @ 2013-08-19 20:40 feiling 阅读(420) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2013-08-19 15:42 feiling 阅读(599) 评论(1) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Top down 的解题方法:1. 将LinkedList的值保存到一个数组中,转化成Convert Sorted Array to Binary Search Tree 来解决时间复杂度为O(n), 空间复杂度为O(n) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 *... 阅读全文
posted @ 2013-08-19 15:19 feiling 阅读(1424) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.[解题思路]递归确定每棵树的根节点的值,这里根节点的值是二分搜索的中值由于这里是有序数组,确定root的时间复杂度为O(1), 整个算法的时间复杂度为O(n),n为节点数目。 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * ... 阅读全文
posted @ 2013-08-19 11:21 feiling 阅读(229) 评论(0) 推荐(0) 编辑