ZXAlgorithm - C7 Two Pointers

Outline
相向双指针
同向双指针

Two Sum
Partition
Sort

0 Templete

同向双指针,相向双指针,Two Sum
链表上的快慢指针
快速排序 & 归并排序
Loop one variable, research the change of another variable, while(start < end)
类型:

  • 背向双指针
    第一节课中的 Longest Palindromic Substring 的中心线枚举算法
    第二节课中的 Find K Closest Elements
  • 相向双指针
    Two Sum 的一大类题(两位数的相关变形题)
    Partition 的一大类题(两位数相关变形题)
  • 同向双指针
    滑动窗口类 Sliding Window
    快慢指针类 Fast & Slow Pointers

1 同向双指针

Lintcode 604.Window Sum
http://www.lintcode.com/problem/window-sum/
https://www.jiuzhang.com/solutions/?search=window%20sum

Leetcode 283.Move zeros
https://leetcode.com/problemset/all/?search=move%20zero
http://www.lintcode.com/problem/move-zeroes/
https://www.jiuzhang.com/solutions/?search=move%20zero
右边指针指向的数字不是0的都堆到左边指针指向的位置
S: Int left = 0, right = 0;
D: left is the index of first 0, right is current index

LeetCode 26.Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
http://www.lintcode.com/problem/remove-duplicate-numbers-in-array/
https://www.jiuzhang.com/solutions/?search=remove%20duplicate%20numbers%20in%20array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/discuss/11751/Simple-Python-solution-O(n)
区分Sorted和Unsorted,如果是sorted用一个pointer标识当前位置,如果是unsorted,还需要一个set来判断是否有重复的
for (Map.Entry<Integer, Boolean> entry : mp.entrySet())
nums[result++] = entry.getKey();

2 相向双指针

两根指针一头一尾,向中间靠拢直到相遇
时间复杂度 O(n)

Leetcode 125/680. Valid Palindrome I/II
验证一个字符串是否为回文串,忽略大小写和非英文字母字符
https://leetcode.com/problemset/all/?search=valid%20palindrome
http://www.lintcode.com/problem/valid-palindrome/
http://www.jiuzhang.com/solution/valid-palindrome/
isalpha()和isdigit()来去掉非想要的字符串
Follow up: 可以删掉一个字符
http://www.lintcode.com/problem/valid-palindrome-ii/
https://www.jiuzhang.com/solution/valid-palindrome-ii/
把is_palindrome抽象成一个函数来复用
Lintcode 8.Rotate string
http://www.lintcode.com/problem/rotate-string/
https://www.jiuzhang.com/solutions/?search=rotate%20string
S: offset = offset % str.length
D: 3 reverse method, careful with offset and place
Left,right and whole
Lintcode 39.Recover rotated sorted array
http://www.lintcode.com/en/problem/recover-rotated-sorted-array/
https://www.jiuzhang.com/solutions/?search=Recover%20rotated%20sorted%20array
三段翻转即可,用双指针in place翻转数组
D: find offset
And then use 3 reverse method, careful with left++ and right-- list.get()/.set(,)
Sort
Arrays.sort(m, Collections.reverseOrder());
Collections.sort(m, Collections.reverseOrder());

3 Two sum

Leetcode 1.Two sum
https://leetcode.com/problems/two-sum/
http://www.lintcode.com/problem/two-sum/ http://www.jiuzhang.com/solutions/two-sum/
哈希表(HashMap) vs 两根指针(Two Pointers)
D: Use two pointer or Hashmap: store negative value in map, and find if anything remain match

Lintcode 607.Two sum III data structure design
http://www.lintcode.com/problem/two-sum-data-structure-design/ https://www.jiuzhang.com/solution/two-sum-iii-data-structure-design/
D: HashMap

Leetcode 167.Two Sum II - Input array is sorted
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
http://www.lintcode.com/problem/two-sum-input-array-is-sorted/ http://www.jiuzhang.com/solutions/two-sum-input-array-is-sorted/
Two Pointers and narrow the range相向指针
D: Two pointers: should be sorted array, and 2 pointer from left to right

Lintcode Unique pairs
http://www.lintcode.com/en/problem/two-sum-unique-pairs/ http://www.jiuzhang.com/solutions/two-sum-unique-pairs/
D: Sort and remove duplicate in the while(left < right), that is remove duplicate before moving pointers
找到相等的时候别停下来,继续找就行。
注意跳过相等元素避免重复方案。

Lintcode 57.Three Sum
http://www.lintcode.com/problem/3sum/
http://www.jiuzhang.com/solutions/3sum/
统计所有的和为 0 的三元组 (Triples)
D: Sort and fix one point(remove duplicate) and do the two sum on the right of the point, because this point is checked, it will not be checked in the next index

Lintcode 382.Triangle Count
http://www.lintcode.com/problem/triangle-count/ http://www.jiuzhang.com/solutions/triangle-count/
S: left, right, ans
D: Fix the one value t = i 0->len-1, set left and right between 0-t find 2 sides > one side then ans = ans + (right - left) remember right and left is index

Two sum计数问题

统计所有和 <= target 的配对数
Lintcode Two Sum - Less than or equal to target
http://www.lintcode.com/problem/two-sum-less-than-or-equal-to-target/ http://www.jiuzhang.com/solutions/two-sum-less-than-or-equal-to-target/
set a count and update count use right - left or left - right
'<=' target
D: use two pointers, when <= target, ans = ans + (right - left)
Lintcode Two Sum - Greater than target
http://www.lintcode.com/en/problem/two-sum-greater-than-target/ http://www.jiuzhang.com/solutions/two-sum-greater-than-target/
'>=' target
D: same as the above , 一个是左边位置,一个是右边
统计所有和 >= target 的配对数

Lintcode Two Sum Closest
http://www.lintcode.com/problem/two-sum-closest-to-target/ http://www.jiuzhang.com/solutions/two-sum-closest/
D: Return the difference, use Math.min()

Follow up:
Lintcode Three Sum Closest
http://www.lintcode.com/problem/3sum-closest/ http://www.jiuzhang.com/solutions/3sum-closest/
loop one value then do the 2 sumclosest

总结

对于求 2 个变量如何组合的问题
可以循环其中一个变量,然后研究另外一个变量如何变化
Trapping Rain Water
https://leetcode.com/problems/trapping-rain-water/
https://leetcode.com/problems/trapping-rain-water/discuss/17554/Share-my-one-pass-Python-solution-with-explaination

5 Partition

Leetcode 31.Partition Array
D: use two pointers, when left > target and right <= target then exchange each other
http://www.lintcode.com/problem/partition-array/ http://www.jiuzhang.com/solutions/partition-array/

Lintcode Quick select
http://www.lintcode.com/problem/kth-smallest-numbers-in-unsorted-array/
https://www.jiuzhang.com/solution/kth-smallest-numbers-in-unsorted-array/
http://www.lintcode.com/problem/kth-largest-element/
https://www.jiuzhang.com/solution/kth-largest-element/
小视频:http://www.jiuzhang.com/video/quick-select/
Kth smallest numbers in unsorted array
D: first set the pivot and get it's place in the array, then you will know the kth is in the left or right
Kth largest element
D: same as above

5 Sort

Quick Sort(binary search)
Quick sort is not stable, merge sort is stable but need O(n) space
Sort Colors
http://www.lintcode.com/problem/sort-colors/ http://www.jiuzhang.com/solutions/sort-colors/ 分成两个部分 vs 分成三个部分
S: pl, pr, i
D: red, white and blue, Two pointers, use I to go through, pl to represent red end, pr to blue start
Rainbow sort
http://www.lintcode.com/en/problem/sort-colors-ii/ http://www.jiuzhang.com/solutions/sort-colors-ii/
S: colors[], left, right, colorFrom, colorTo
D: Use quick sort by 1-k, use colarMid = (colorFrom + colorTo)/2 as pivot

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352