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

导航