Two Pointers
文章平均质量分 67
Dylan_Java_NYC
练很重要,总结更重要,感谢优快云给了我这么好的平台交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode Longest Substring Without Repeating Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-without-repeating-characters/ String 题目,基本思路就是维护一个窗口[walker,runner]. walker,runner都是向右移,同时维护一个HashSet hs,用来存储这一段中的所有不同character。首先右侧窗口runner在前跑原创 2015-08-08 10:30:16 · 533 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array II
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 本题是Remove Duplicates from Sorted Array的延伸。不同就是允许一次重复,所以可以加一个limit,当limit小于1的时候可以,同时limit++, limit等于1时跳过。注意这里的条件是小于1而不是2原创 2015-09-02 13:01:43 · 531 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List II
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 本题与Remove Duplicates from Sorted List相似,不同就是要完全去掉duplicate. 所以要维护一个前指针, 当cur.val == cur.next.val 时,就一直移动cur直到不同,然后pre的n原创 2015-09-02 12:25:56 · 726 阅读 · 0 评论 -
LeetCode Linked List Cycle II
原题链接在这里:https://leetcode.com/problems/linked-list-cycle-ii/ 首先找到是否有cycle,若是没有返回null。 若是有cycle,可以从head,用two pointers一个一个试直到找到cycle beginner,Time O(n^2). 另外一种方法参见了这篇帖子,是数学想法。 AC Java: /** * Defini原创 2015-09-01 03:45:18 · 360 阅读 · 0 评论 -
LeetCode Linked List Cycle
原题链接在这里:https://leetcode.com/problems/linked-list-cycle/ 两个指针,一快runner,每次走两步;一慢walker,每次走一步,若是它俩相遇就是有cycle。 Note: 这里的while loop 终止条件是runner!=null && runner.next!=null, 因为下面会用到 runner.next.next. 这类原创 2015-08-29 22:12:16 · 309 阅读 · 0 评论 -
LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路: 1. 找到距离各自tail 相同距离的起始ListNode,可以通过两个pointer,长的那个先移动Math.abs(lenA-lenB). 2. 两个pointer各自移动直到找到相同的ListNode. Node: 1. 若有inter原创 2015-08-29 04:07:21 · 481 阅读 · 0 评论 -
LeetCode Palindrome Linked List
原题链接在这里:https://leetcode.com/problems/palindrome-linked-list/ 思路: 原题要求time O(n), space O(1). 所以不能用额外空间。先找到中点,reverse中点后面的list部分,再与head开始逐个比较 val. 期中reverse部分可以参见Reverse Linked List中的Method 2. AC Ja原创 2015-08-29 03:19:01 · 282 阅读 · 0 评论 -
LeetCode Remove Nth Node From End of List
原题链接在这里:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Method 1: 算出总长度,再减去n,即为要从头多动的点。但要新要求,only one pass。 Method 2: 两个指针,一个runner,一个walker,runner先走n步,随后runner和walker一起走,直到runner指为空原创 2015-08-29 01:00:18 · 237 阅读 · 0 评论 -
LeetCode Minimum Window Substring
原题链接在这里: https://leetcode.com/problems/minimum-window-substring/ 又是一道双指针问题,与Longest Substring Without Repeating Characters, Substring with Concatenation of All Words相似。 维护一个字典和一个窗口,不同的地方是题目说contai原创 2015-08-13 04:51:50 · 409 阅读 · 0 评论 -
LeetCode Substring with Concatenation of All Words
原题链接在这里: https://leetcode.com/problems/substring-with-concatenation-of-all-words/ 这道题思路与Longest Substring Without Repeating Characters 相似。 不同的是这道题需要先生成并维护一个字典,检查新词是否是字典里的词。同时维护一个窗口[start,end], 外层循环原创 2015-08-12 10:14:05 · 429 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 利用counter来更改新array前面的元素,因为原题中知名length后面的元素没有关系。 AC Java: public class Solution { public int removeDuplicates(int[]原创 2015-09-02 12:38:00 · 413 阅读 · 0 评论
分享