摘要: 这道题,采用动态规划算法。from top to down,把到每个节点的最小路径和都求出来。下面是AC代码: 1 /** 2 * Given a triangle, find the minimum path sum from top to bottom. 3 * Each ... 阅读全文
posted @ 2014-05-01 23:26 echoht 阅读(147) 评论(0) 推荐(0) 编辑
摘要: 采用2项公式解这问题挺简单,唯一需要注意的是当两个比较大的数相乘时,容易越界,我采用分子、分母同时除以他们的最大公约数。下面是AC代码: 1 /** 2 * Given an index k, return the kth row of the Pascal's triangle. 3 ... 阅读全文
posted @ 2014-05-01 20:13 echoht 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 这道题是生成杨辉三角,看了下杨辉三角的性质,就可以解决了。下面是AC代码: 1 /** 2 * Given numRows, generate the first numRows of Pascal's triangle. 3 * @param numRows 4 * ... 阅读全文
posted @ 2014-05-01 18:37 echoht 阅读(177) 评论(0) 推荐(0) 编辑
摘要: 这道题在前两个的基础上做稍微改进就可以。下面是AC代码: 1 /** 2 * Design an algorithm to find the maximum profit. You may complete at most two transactions. 3 * @pa... 阅读全文
posted @ 2014-05-01 17:54 echoht 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 这道题我一开始想到用递归方法,可以把规模大的问题变成规模小的问题,但是觉得递归的时间复杂度很高,因为它会把相同的问题进行重复计算,然后我想是不是有什么down-up的方法,先把所有的子问题的结果保存起来,但是发现问题的最优解并不能由子问题的最优解推导出来。最后就想到买股票的时候,我们在一个局部极小的... 阅读全文
posted @ 2014-05-01 15:02 echoht 阅读(166) 评论(0) 推荐(0) 编辑