上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 43 下一页
摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.[解题思路]与Unique Paths类似,只是把求路径数改成求最小路径和目标函数:sum[i][j] = grid[i][j] + Math.min(sum[i+1][j], sum 阅读全文
posted @ 2013-08-20 22:43 feiling 阅读(273) 评论(0) 推荐(0) 编辑
摘要: 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 is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文
posted @ 2013-08-20 22:09 feiling 阅读(363) 评论(0) 推荐(0) 编辑
摘要: 1.DP bottom up 1 public int uniquePaths(int m, int n) { 2 // Start typing your Java solution below 3 // DO NOT write main() function 4 int[][] steps = new int[m+2][n+2]; 5 for(int i = 0; i = 1; i--){13 for(int j = n; j >= 1; j--){14 steps[i... 阅读全文
posted @ 2013-08-20 17:15 feiling 阅读(467) 评论(0) 推荐(0) 编辑
摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you devise a constant space so 阅读全文
posted @ 2013-08-20 10:46 feiling 阅读(327) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文
posted @ 2013-08-20 10:21 feiling 阅读(294) 评论(0) 推荐(0) 编辑
摘要: 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) 编辑
上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 43 下一页