算法與數據結構基礎 - 雙指針(Two Pointers)

雙指針基礎html

雙指針(Two Pointers)是面對數組、鏈表結構的一種處理技巧。這裏「指針」是泛指,不但包括一般意義上的指針,還包括索引、迭代器等可用於遍歷的遊標。 node

 

同方向指針git

設定兩個指針、從頭往尾(或從尾到頭)遍歷,我稱之爲同方向指針,第一個指針用於遍歷,第二個指針知足必定條件下移動。例如 LeetCode題目 283. Move Zeroes:github

    // 283. Move Zeroes
    void moveZeroes(vector<int>& nums) {
        int i=0;
        for(int j=0;j<nums.size();j++){
            if(nums[j]!=0) nums[i++]=nums[j];
        }
        while(i<nums.size()) nums[i++]=0;
    }

 

相關LeetCode題:算法

283. Move Zeroes  題解數組

26. Remove Duplicates from Sorted Array  題解數據結構

80. Remove Duplicates from Sorted Array II  題解app

349. Intersection of Two Arrays  題解less

350. Intersection of Two Arrays II  題解dom

925. Long Pressed Name  題解

88. Merge Sorted Array  題解

844. Backspace String Compare  題解

86. Partition List  題解

986. Interval List Intersections  題解

209. Minimum Size Subarray Sum  題解

713. Subarray Product Less Than K  題解

826. Most Profit Assigning Work  題解

930. Binary Subarrays With Sum  題解

838. Push Dominoes  題解

 

滑動窗口(Sliding Windows)也屬於同方向指針,關於滑動窗口詳見:

算法與數據結構基礎 - 滑動窗口(Sliding Window)

 

快慢指針

若雙指針以固定步長移動,如第一個指針移動兩步、第二個指針移動一步,這種咱們稱之爲快慢指針。快慢指針經常使用於單向鏈表環(Cycle)判斷、Loop判斷等問題。

 

相關LeetCode題:

19. Remove Nth Node From End of List  題解

141. Linked List Cycle  題解

142. Linked List Cycle II  題解

234. Palindrome Linked List  題解

457. Circular Array Loop  題解

287. Find the Duplicate Number  題解

 

反方向指針

若雙指針其中一個從頭開始、另外一個從尾開始,二者往中間遍歷,這種使用方法我稱之爲反方向指針。例如常見的反轉字符串問題 LeetCode 344. Reverse String:

    // 344. Reverse String 
    void reverseString(vector<char>& s) {
        int i=0,j=s.size()-1;
        while(i<j) swap(s[i++],s[j--]);
    }

 

相關LeetCode題:

344. Reverse String  題解

125. Valid Palindrome  題解

345. Reverse Vowels of a String  題解

61. Rotate List  題解

75. Sort Colors  題解

1093. Statistics from a Large Sample  題解

11. Container With Most Water  題解

42. Trapping Rain Water  題解

 

應用於有序數列

一些狀況下先對數組排序,利用有序這個性質來判別雙指針怎麼移動,例如 LeetCode題目 15. 3Sum:

            // 15. 3Sum
int l=i+1, r=nums.size()-1; while(l<r){ int tmp=nums[i]+nums[l]+nums[r]; if(tmp>0) r--; else if(tmp<0) l++; …… }

 

相關LeetCode題:

977. Squares of a Sorted Array  題解

360. Sort Transformed Array  題解

532. K-diff Pairs in an Array  題解

881. Boats to Save People  題解

167. Two Sum II - Input array is sorted  題解

15. 3Sum  題解

16. 3Sum Closest  題解

259. 3Sum Smaller  題解

923. 3Sum With Multiplicity  題解

18. 4Sum  題解

相關文章
相關標籤/搜索