摘要: 简单题。从尾巴开始往前就行了。public class Solution { public void merge(int A[], int m, int B[], int n) { // Start typing your Java solution below // DO NOT write main() function int len = m + n; int i = m - 1; int j = n - 1; int k = len - 1; while (i >= ... 阅读全文
posted @ 2013-08-10 10:37 阿牧遥 阅读(202) 评论(0) 推荐(0)
摘要: 此题甚难。后来看了参考才做出来的。基本的思路就是,在递归中计算包含该root的最大值并更新至max[0](Java无法按引用传,就只能建立一个数组or something)。包含该root的最大值有如下几种可能:1.root本身;2.root和左子树中一条路径;3.root和右子树中一条路径;4.左子树一条路径和root和右子树一条路径。其中取最大就可更新至max[0]其中1,2,3可用来计算上一级的root的最大值,所以要传回去。最终,对于最上层的root来说,数内的最大路径不一定要经过根,但由于每个节点都遍历到,其最大值已经存在max[0]里面了。/** * Definition for 阅读全文
posted @ 2013-08-10 10:24 阿牧遥 阅读(1391) 评论(0) 推荐(0)
摘要: 一开始用递归。大数据超时了。后来网上看了一下,是动态规划。归根结底就是有重复子问题,比如bbbbaaaa由bbaa和bbaa来匹配的话,那么无论第一个b选s1还是s2的,都会出现baa和baa这样的子问题。那么由于s1+s2==s3,只需要用二维数组记录状态就行了。状态转移方程可详见:http://blog.unieagle.net/2012/09/29/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ainterleaving-string%EF%BC%8C%E4%BA%8C%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/这是最 阅读全文
posted @ 2013-08-09 21:34 阿牧遥 阅读(426) 评论(0) 推荐(0)
摘要: 有意思的是还要写非递归版本。这是递归版本:public class Solution { public boolean isSymmetric(TreeNode root) { // Start typing your Java solution below // DO NOT write main() function if (root == null) return true; return (isMirror(root.left, root.right)); } public boolean isMirro... 阅读全文
posted @ 2013-08-08 23:32 阿牧遥 阅读(175) 评论(0) 推荐(0)
摘要: 简单题,树的递归。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { // Start typing your Java solution below ... 阅读全文
posted @ 2013-08-08 23:12 阿牧遥 阅读(1134) 评论(0) 推荐(1)
摘要: 树的递归。要注意对左右子树中有null的处理。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDepth(TreeNode root) { // Start typing your Java solution below ... 阅读全文
posted @ 2013-08-08 23:07 阿牧遥 阅读(226) 评论(0) 推荐(0)
摘要: 简单题。现在对于这种递归已经很熟练了啊。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ArrayList> pathSum(TreeNode root, int sum) { // Start typing your Java solution b... 阅读全文
posted @ 2013-08-08 22:58 阿牧遥 阅读(174) 评论(0) 推荐(0)
摘要: 简单题。递归。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean hasPathSum(TreeNode root, int sum) { // Start typing your Java solution below /... 阅读全文
posted @ 2013-08-08 13:42 阿牧遥 阅读(202) 评论(0) 推荐(0)
摘要: 简单题。进位加法,或者数九就可以了。 public class Solution { public int[] plusOne(int[] digits) { // Start typing your Java solution below // DO NOT write main() functi 阅读全文
posted @ 2013-08-08 13:36 阿牧遥 阅读(188) 评论(0) 推荐(0)
摘要: 又是一个DFS。犯过的一个错误是,上下左右移动的时候,不需要x,y一起变,只要变一个就行了。在参考答案里面,把访问过得格子标为'#'特殊字符,就可以省去visited数组,但这样会改变原来的数组。http://discuss.leetcode.com/questions/254/word-searchpublic class Solution { public boolean exist(char[][] board, String word) { // Start typing your Java solution below // DO NOT write... 阅读全文
posted @ 2013-08-07 23:14 阿牧遥 阅读(306) 评论(0) 推荐(0)