- 博客(5)
- 收藏
- 关注
原创 Unique Binary Search Trees
思路:可用递归,由大化小;也可使用迭代;也可投机取巧,由公式直接得出; 方法一:投机取巧,由组合公式2n中取n个的排列个数/(n+1),即为二叉搜索树形态个数。 public class Solution { public int numTrees(int n) { java.math.BigInteger sum1=java.math.BigInteger.ONE,s
2014-11-28 09:41:46
170
原创 Best Time to Buy and Sell Stock II
思路:一支股票一天内买卖多次且必须先买后卖,那么可以作如下操作: (1)若第i+1天的股票价格>第i天的股票价格,则以该价格卖出,再以该价格买进,获利=第i+1天的股票价格-第i天的股票价格; (2)否则,放弃第i天的股票,买进第i+1天的股票,等待升值 因此,遍历数组一遍,相邻元素后者大于前者的差值之和即为最大利润。 public class Solution { public
2014-11-27 16:54:17
364
原创 Same Tree
思路:递归 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution {
2014-11-27 16:07:24
323
原创 Maximum Depth of Binary Tree
思路:递归树的深度即为所求。 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solu
2014-11-27 12:50:46
172
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人