2014年3月9日

LeetCode: Longest Consecutive Sequence

摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.这个题想了半天,没有想到合适的方法。如何能不排序,还能找到连续子序列呢。。。后来看网上写的,就知道怎么做了。用hash表,将每个元素保存下来。然后判断每个元素的左右两侧,是否有连续的序列。需要注意的是,为了避免重复检查,需要把每次处理过的元素删除。 1 public class Solution { 2 public int longestConsecutive(int[] num) { 3 ... 阅读全文

posted @ 2014-03-09 13:16 longhorn 阅读(168) 评论(0) 推荐(0) 编辑

LeetCode: Clone Graph

摘要: 1 public class Solution { 2 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 3 Map map = new HashMap(); 4 Queue nodes = new LinkedList(); 5 if (node == null) return node; 6 7 UndirectedGraphNode head = new UndirectedGraphNode(node.label)... 阅读全文

posted @ 2014-03-09 03:07 longhorn 阅读(246) 评论(0) 推荐(0) 编辑

2014年3月3日

Multiply two big numbers

摘要: http://www.geeksforgeeks.org/divide-and-conquer-set-2-karatsuba-algorithm-for-fast-multiplication/ 阅读全文

posted @ 2014-03-03 04:44 longhorn 阅读(129) 评论(0) 推荐(0) 编辑

2014年3月2日

Lowest Common Ancestor of a Binary Tree & Binary Search Tree

摘要: 这里有两种情况,一种是Binary Tree,一种是Binary Search Tree。Binary Search Tree1 Node *LCA(Node *root, Node *p, Node *q) {2 if (!root || !p || !q) return NULL;3 if (max(p->data, q->data) data)4 return LCA(root->left, p, q);5 else if (min(p->data, q->data) > root->data)6 return LCA(root->righ 阅读全文

posted @ 2014-03-02 12:28 longhorn 阅读(213) 评论(0) 推荐(0) 编辑

2014年2月13日

Binary Search

摘要: 在有重复元素的条件下如何进行binary search?原来的方法会返回重复元素中随机的一个。如何返回第一个target或者最后一个? 1 public static int binarySearch(int[] A, int target) { 2 int start = 0; 3 int end = A.length-1; 4 int mid = 0; 5 6 int result = -1; 7 8 while (start target) {16 ... 阅读全文

posted @ 2014-02-13 01:38 longhorn 阅读(213) 评论(0) 推荐(0) 编辑

LeetCode: Search in Rotated Sorted Array I & II

摘要: 首先要注意二分法。二分法的终止条件是:low > high,而不是low >= high。因为在某些情况下,low == high的下一步就是就是low > high,而low == high恰是范围缩小到一个元素的情况。二分法的更新操作。是将mid-1赋值给high,或者将mid+1赋值给low。而不是mid赋值给high或low。当只有2个元素的时候,会形成无线循环。所以每次要缩小范围。这道题一共做了三种算法。第一种是用二分法先找到数组中的最大元素,确定两段数组分别的位置。然后再根据target的大小确定它在哪个范围内,进而在其中一段再进行二分查找。存在的问题是,找最大元 阅读全文

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

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) 编辑

导航