2014年2月8日

LeetCode: Reorder List

摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.我想到的方法是用stack记录下要处理的node,就是从中间开始,后面的node都压入stack。然后再把他们一一插入到原来的list中。比较需要小心的地方时,节点个数为奇数还是偶数的情况不同。因为要给最后 阅读全文

posted @ 2014-02-08 10:22 longhorn 阅读(178) 评论(0) 推荐(0) 编辑

LeetCode: Linked List Cycle I & II

摘要: Given a linked list, determine if it has a cycle in it.想了好半天没想出来,后来看网上的做法。用快慢两个指针,慢指针每次移动一位,快指针每次移动两位。如果存在环的话,两个指针一定会相遇。最差的情况是,在慢指针进入环的时候,快指针恰巧在慢指针前面一位,如果环的长度为k<=n的话,这时需要n-1步,两个指针能再次相遇。O(n)。加上慢指针进入环之前的时间O(n)。所以整个算法的时间复杂度为—O(n)。 1 public boolean hasCycle(ListNode head) { 2 ListNode slow = hea... 阅读全文

posted @ 2014-02-08 04:59 longhorn 阅读(251) 评论(0) 推荐(0) 编辑

2014年2月6日

LeetCode: Copy List with Random Pointer

摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.我想到了一个时间O(n),空间O(n)的算法。关键要注意的地方是,头节点一定不要忘了放入hashmap中,最后一个节点的next赋值为null。Method 1 (Uses O(n) extra space)This method stores the next and ar 阅读全文

posted @ 2014-02-06 13:20 longhorn 阅读(160) 评论(0) 推荐(0) 编辑

2014年2月4日

LeetCode: Gas Station

摘要: There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas 阅读全文

posted @ 2014-02-04 05:19 longhorn 阅读(213) 评论(0) 推荐(0) 编辑

LeetCode: Sum Root to Leaf Numbers

摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.思路我觉得就是递归,如果遇到叶节点,返回叶节点的值,否则返回两个节点相加的结果。注意的是,我本来想用一个全局的静态的值表示总和,但是在leetcode 阅读全文

posted @ 2014-02-04 03:31 longhorn 阅读(209) 评论(0) 推荐(0) 编辑

2014年2月3日

LeetCode: Combination Sum II

摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.这里数字不需要重复出现,所以使用dfs的时候,循环从上一次的下一个位置开始就可以了。但是这样会出现重复的结果。比如[1,2,3,1],因为第一个位置可以选两次1,所以可能会出现重复结果。所以这里排序是为了方便避免重复 阅读全文

posted @ 2014-02-03 14:03 longhorn 阅读(254) 评论(0) 推荐(0) 编辑

LeetCode: Combination Sum

摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.找到全部解,一看就是一个dfs的节奏。dfs需要判断循环起始的位置。需要注意的地方是,因为一个数字可以无限次的出现,所以每次dfs的起始位置不是上一次加1,而是与上一次相同,这样的话就可以重复选一个数字 阅读全文

posted @ 2014-02-03 13:58 longhorn 阅读(159) 评论(0) 推荐(0) 编辑

LeetCode: Palindrome Partitioning II

摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.这种求最优解的问题就不适合用dfs做了,最优解一般都是DP,比遍历所有情况比较快。这道题的dp很好想,就是i之前的最优解=min(j之前的最优解+1) j result[j] + 1) min = result[j]+1;11 }12 }13... 阅读全文

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

LeetCode: Palindrome Partitioning

摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.像这种求所有可能性的问题就不适合用DP做,比较适合dfs。思路就是从头扫描,按顺序将palindrome放入结果中。需要注意的是,这里判断是否为palindrome的方法是,将string反转,然后比较和原来的是否相同。 1 public static ArrayList> partition(String s) { 2 . 阅读全文

posted @ 2014-02-03 13:17 longhorn 阅读(165) 评论(0) 推荐(0) 编辑

LeetCode: Anagrams

摘要: Given an array of strings, return all groups of strings that are anagrams.第一次面试dropbox就是面的anagrams。在面试前还恰巧看到了这个题,最后答得还是一塌糊涂。我在面试的时候想到了对string字符排序,但我忘记了hashtable这回事了。这个题就是把string的字符排下序,然后放到一个hashmap中,如果有其他string和这个能匹配上,就放到result中;否则放入map中。就这么简单。。。。哎。。。。 1 public ArrayList anagrams(String[] strs) { 2 阅读全文

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

导航