上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 43 下一页
摘要: 用递归就比较简单。class Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { ListNode* current = head; ListNode** nodeArray = new ListNode*[k]; int i = 0; while (current != NULL && i next; i++; } if (i 0; j--) { nodeArra... 阅读全文
posted @ 2013-09-08 23:32 阿牧遥 阅读(231) 评论(0) 推荐(0)
摘要: LeetCode抽风,用Java会runtime error就用c++来写。思路是简单的,写法参考了答案。用了root和pivot两个辅助头结点,就可以免去对是否为NULL的判断。还有要对尾节点的next设为NULL。class Solution {public: ListNode *partition(ListNode *head, int x) { ListNode* root = new ListNode(-1); ListNode* pivot = new ListNode(-1); ListNode* root_last = root;... 阅读全文
posted @ 2013-09-08 23:12 阿牧遥 阅读(305) 评论(0) 推荐(1)
摘要: 捡个软柿子捏一捏,结果也有不少小坑。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)
摘要: 此题要多品品。由于是求最短的路径,所以用BFS,DFS会在大数据的时候超时,原因也容易理解。最终解法参考了:http://discuss.leetcode.com/questions/1108/word-ladder和http://blog.sina.com.cn/s/blog_b9285de201... 阅读全文
posted @ 2013-08-31 21:43 阿牧遥 阅读(427) 评论(0) 推荐(0)
摘要: 相对简单,就是从两边往中间推进,忽略其他字符。 public class Solution { public boolean isPalindrome(String s) { if (s.length() == 0) return true; int i = 0; int j = s.length( 阅读全文
posted @ 2013-08-31 15:22 阿牧遥 阅读(164) 评论(0) 推荐(0)
上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 43 下一页