摘要: 捡个软柿子捏一捏,结果也有不少小坑。1.n可能会比length大(或相等),所以要先%一下;2. 是向右rotate而不是向左,所以先n=len-npublic class Solution { public ListNode rotateRight(ListNode head, int n) { if (head == null) return null; int len = 0; ListNode node = head; while (node != null) { node = node.... 阅读全文
posted @ 2013-09-03 17:07 阿牧遥 阅读(187) 评论(0) 推荐(0)
摘要: 双指针,简单题,顺手就写出来了。public class Solution { public int removeElement(int[] A, int elem) { int i = 0; int j = 0; int len = A.length; while (j < len) { if (A[j] == elem) { j++; } else { ... 阅读全文
posted @ 2013-09-03 16:43 阿牧遥 阅读(155) 评论(0) 推荐(0)
摘要: 犯了两个错误,一个是node为null时,仍然把left和right往queue里放,空指针错误。一个是到最后一层,节点的left和right都是null,那么新建的ArrayList就是空,不该往结果里放。此题DFS也可,就是把层次传进去:http://discuss.leetcode.com/questions/275/binary-tree-level-order-traversal-iipublic class Solution { public ArrayList> levelOrderBottom(TreeNode root) { ArrayList> ans ... 阅读全文
posted @ 2013-09-03 16:36 阿牧遥 阅读(182) 评论(0) 推荐(0)
摘要: 这道题目由于之前那题的训练,思路来的很快,但写的时候脑子浆糊了。这个时候就要高屋建瓴的去思考,把其中一段循环什么啊提炼成一个函数出来,那么主要那一块的逻辑就简单了。public class Solution { public void connect(TreeLinkNode root) { if (root != null) root.next = null; TreeLinkNode first = root; TreeLinkNode last = null; while (first != null) { ... 阅读全文
posted @ 2013-09-03 16:01 阿牧遥 阅读(284) 评论(0) 推荐(0)
摘要: 简单的递归。命名上用了inLow, inHigh这样的觉得比较好。public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if (inorder.length != postorder.length) return null; return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); } private TreeNode bu... 阅读全文
posted @ 2013-09-03 15:21 阿牧遥 阅读(195) 评论(0) 推荐(0)
摘要: 用递归比较方便。public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder.length != inorder.length) return null; return buildTree(preorder, 0, preorder.length, inorder, 0, inorder.length); } private TreeNode buildTree(in... 阅读全文
posted @ 2013-09-03 14:58 阿牧遥 阅读(214) 评论(0) 推荐(0)