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)
類型:

  • 背向雙指針
    第一節(jié)課中的 Longest Palindromic Substring 的中心線枚舉算法
    第二節(jié)課中的 Find K Closest Elements
  • 相向雙指針
    Two Sum 的一大類題(兩位數(shù)的相關(guān)變形題)
    Partition 的一大類題(兩位數(shù)相關(guān)變形題)
  • 同向雙指針
    滑動(dòng)窗口類 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
右邊指針指向的數(shù)字不是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)
區(qū)分Sorted和Unsorted卷扮,如果是sorted用一個(gè)pointer標(biāo)識(shí)當(dāng)前位置期揪,如果是unsorted碎捺,還需要一個(gè)set來(lái)判斷是否有重復(fù)的
for (Map.Entry<Integer, Boolean> entry : mp.entrySet())
nums[result++] = entry.getKey();

2 相向雙指針

兩根指針一頭一尾彤枢,向中間靠攏直到相遇
時(shí)間復(fù)雜度 O(n)

Leetcode 125/680. Valid Palindrome I/II
驗(yàn)證一個(gè)字符串是否為回文串苍苞,忽略大小寫和非英文字母字符
https://leetcode.com/problemset/all/?search=valid%20palindrome
http://www.lintcode.com/problem/valid-palindrome/
http://www.jiuzhang.com/solution/valid-palindrome/
isalpha()和isdigit()來(lái)去掉非想要的字符串
Follow up: 可以刪掉一個(gè)字符
http://www.lintcode.com/problem/valid-palindrome-ii/
https://www.jiuzhang.com/solution/valid-palindrome-ii/
把is_palindrome抽象成一個(gè)函數(shù)來(lái)復(fù)用
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
三段翻轉(zhuǎn)即可固翰,用雙指針in place翻轉(zhuǎn)數(shù)組
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
找到相等的時(shí)候別停下來(lái),繼續(xù)找就行羹呵。
注意跳過(guò)相等元素避免重復(fù)方案骂际。

Lintcode 57.Three Sum
http://www.lintcode.com/problem/3sum/
http://www.jiuzhang.com/solutions/3sum/
統(tǒng)計(jì)所有的和為 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計(jì)數(shù)問(wèn)題

統(tǒng)計(jì)所有和 <= target 的配對(duì)數(shù)
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 , 一個(gè)是左邊位置,一個(gè)是右邊
統(tǒng)計(jì)所有和 >= target 的配對(duì)數(shù)

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

總結(jié)

對(duì)于求 2 個(gè)變量如何組合的問(wèn)題
可以循環(huán)其中一個(gè)變量冈欢,然后研究另外一個(gè)變量如何變化
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/ 分成兩個(gè)部分 vs 分成三個(gè)部分
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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末歉铝,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子凑耻,更是在濱河造成了極大的恐慌太示,老刑警劉巖柠贤,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異先匪,居然都是意外死亡种吸,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門呀非,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)坚俗,“玉大人,你說(shuō)我怎么就攤上這事岸裙〔埽” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵降允,是天一觀的道長(zhǎng)恩闻。 經(jīng)常有香客問(wèn)我,道長(zhǎng)剧董,這世上最難降的妖魔是什么幢尚? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮翅楼,結(jié)果婚禮上尉剩,老公的妹妹穿的比我還像新娘。我一直安慰自己毅臊,他們只是感情好理茎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著管嬉,像睡著了一般皂林。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蚯撩,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天础倍,我揣著相機(jī)與錄音,去河邊找鬼胎挎。 笑死著隆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的呀癣。 我是一名探鬼主播美浦,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼项栏!你這毒婦竟也來(lái)了浦辨?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎流酬,沒(méi)想到半個(gè)月后币厕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡芽腾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年旦装,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片摊滔。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡阴绢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出艰躺,到底是詐尸還是另有隱情呻袭,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布腺兴,位于F島的核電站左电,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏页响。R本人自食惡果不足惜篓足,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望闰蚕。 院中可真熱鬧纷纫,春花似錦、人聲如沸陪腌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)诗鸭。三九已至,卻和暖如春参滴,著一層夾襖步出監(jiān)牢的瞬間强岸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工砾赔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蝌箍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓暴心,卻偏偏與公主長(zhǎng)得像妓盲,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子专普,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355