2014年2月10日

LeetCode: Flatten Binary Tree to Linked List

摘要: Given a binary tree, flatten it to a linked list in-place我的想法是,先将左子树flatten,然后右子树flatten,然后将左子树的结果移到root.right,然后将右子树接在左子树后面。但是总是超时。 1 public static void flatten(TreeNode root) { 2 if (root == null) return; 3 TreeNode left = root.left; 4 TreeNode right = root.right; 5 ... 阅读全文

posted @ 2014-02-10 05:37 longhorn 阅读(210) 评论(0) 推荐(0) 编辑

LeetCode: Remove Nth Node From End of List

摘要: Given a linked list, remove thenthnode from the end of list and return its head.原来的想法很简单,就是先扫描一遍list,判断list的长度,然后从头重新扫描,计算好走多少不,找到要删除的node的前一个node,然后把node.next = node.next.next; 这里需要对list扫描两次。 1 public ListNode removeNthFromEnd(ListNode head, int n) { 2 int length = 0; 3 ListNode tmp... 阅读全文

posted @ 2014-02-10 04:33 longhorn 阅读(182) 评论(0) 推荐(0) 编辑

LeetCode: Palindrome Number

摘要: Determine whether an integer is a palindrome. Do this without extra space.题很简单,但我做的不好。我最开始竟然没有想到把这个数反过来,然后判断是否和原来相等这种方法。我最开始竟认为这么做不出来。。。。然后我就想用第一位与最后一位比较是否相等。然后再比较第二位最倒数第二位。后来感觉写的太乱了,也没通过测试。最后用数字转为String的方法做了一个。然后上网看了看,意识到原来把数字反过来是可以判断palindrome的。。。然后写了一个。然后发现网上说,这么做可能会overflow (在reverse Integer中也是这 阅读全文

posted @ 2014-02-10 03:30 longhorn 阅读(202) 评论(0) 推荐(0) 编辑

LeetCode: Remove Duplicates from Sorted Array I & II

摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文

posted @ 2014-02-10 00:58 longhorn 阅读(184) 评论(0) 推荐(0) 编辑

导航