摘要: 找到相遇的点之后,只要让两个指针分别从head 和相遇点出发,走同样多的步数再相遇,就找到了链接开始的地方了。要画个证明一下。public class Solution { public ListNode detectCycle(ListNode head) { if(head ... 阅读全文
posted @ 2014-12-29 06:08 江南第一少 阅读(69) 评论(0) 推荐(0)
摘要: 经典题,送分题,做过无数遍。public class Solution { public boolean hasCycle(ListNode head) { if(head == null) return false; ListNode slow = head; ... 阅读全文
posted @ 2014-12-29 05:37 江南第一少 阅读(88) 评论(0) 推荐(0)
摘要: 本题跟一般DFS不同的是,需要先利用Word Break I 的结果来判断是否能Break,如果不能break,直接裸上DFS,会超时。check 完是否能Break之后就跟一般DFS 一样了。(一般来说,如果不check且不能break,DFS在忙活一阵之后会返回一个空List,但OJ贱贱的有一个... 阅读全文
posted @ 2014-12-29 05:13 江南第一少 阅读(125) 评论(0) 推荐(0)
摘要: dp, 用个数组记下前i个字符是否能被break,而递推公式是:如果已知dp[0] ~ dp[i], 对从0~i 中任意 j,如果有dp[j]&&s.substring(j, i+1) 为true,则dp[i+1] 为true。public class Solution { public bo... 阅读全文
posted @ 2014-12-29 02:48 江南第一少 阅读(117) 评论(0) 推荐(0)
摘要: 跟Clone Graph的实现细节上有些相似的地方,都要用个map记住已存在结点和新建结点之间的映射。循环两遍,空间O(N).public class Solution { public RandomListNode copyRandomList(RandomListNode head) { ... 阅读全文
posted @ 2014-12-29 01:58 江南第一少 阅读(98) 评论(0) 推荐(0)
摘要: 参考链接1和参考链接2public class Solution { public int singleNumber(int[] A) { int[] count = new int[32]; for(int i = 0; i > j) & 1; ... 阅读全文
posted @ 2014-12-28 13:03 江南第一少 阅读(88) 评论(0) 推荐(0)
摘要: public class Solution { public int singleNumber(int[] A) { int result = 0; for(int i = 0; i < A.length; i++) { result = re... 阅读全文
posted @ 2014-12-28 13:01 江南第一少 阅读(99) 评论(0) 推荐(0)
摘要: 两个数组,从两边扫描。题目中,如果rating相等的时候,不要求得到的糖一样,不是很科学。真正面试时要问清楚requirement。public class Solution { public int candy(int[] ratings) { if(ratings == nu... 阅读全文
posted @ 2014-12-28 12:23 江南第一少 阅读(130) 评论(0) 推荐(0)
摘要: 三年前找工作时应该做过这题,现在完全不记得了。参考链接public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { if(gas == null || cost == null || ... 阅读全文
posted @ 2014-12-28 11:57 江南第一少 阅读(120) 评论(0) 推荐(0)
摘要: 只写了DFS递归版,以后复习时再写BFS,DFS非递归版,戳参考链接。public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null... 阅读全文
posted @ 2014-12-28 11:03 江南第一少 阅读(81) 评论(0) 推荐(0)