雙指標法相關的題

張曉寅發表於2020-10-31

  力扣-27. 移除元素

題目連結:https://leetcode-cn.com/problems/remove-element/

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        //雙指標法
        int slowIndex = 0;
        for(int fastIndex = 0;fastIndex < nums.size();fastIndex++){
            if(nums[fastIndex] != val){
                nums[slowIndex++] = nums[fastIndex];
            }
        }
        return slowIndex;
    }
};

力扣-167.兩數之和 II -輸入有序陣列

題目連結:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int left = 0;
        int right = numbers.size()-1;
        while(left < right){
            int sum = numbers[left] + numbers[right];
            if(sum == target)
                return {left+1,right+1};
            else if(sum > target)
                right--;
            else
                left++;
        }
        return {};
    }
};

力扣-977.有序陣列的平方

題目連結:https://leetcode-cn.com/problems/squares-of-a-sorted-array/

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        int n = A.size();
        int left = 0;
        int right = n-1;
        int idex = n-1;
        vector<int> result;
        result.resize(n);
        while(left <= right){
            if(A[left] * A[left] > A[right] * A[right]){
                result[idex--] = A[left]*A[left];
                left++;
            }else{
                result[idex--] = A[right]*A[right];
                right--;
            }
        }
        return result;
    }
};

劍指offer-24. 反轉連結串列

題目連結:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/

//雙指標法:
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;  //雙指標
        ListNode* cur = head;
        while(cur){
            ListNode* next = cur->next; //定義臨時變數儲存下一節點
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
};
//遞迴方法:
class Solution {
public:
    ListNode* reverse(ListNode* cur,ListNode* pre){
        if(cur == nullptr)  //終止條件
            return pre;
        //遞迴函式單層處理函式    
        ListNode* next = cur->next; 
        cur->next = pre;
        pre = cur;
        cur = next;
        return reverse(cur,pre); //將pre作為頭結點一層一層往上return
    }
    ListNode* reverseList(ListNode* head) {
        return reverse(head,nullptr);
    }
};

力扣-283.移動0

題目連結:https://leetcode-cn.com/problems/move-zeroes/

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        if(nums.size() < 2)
            return;
        int i = 0;
        int j = 1; //定義兩個索引
        while(i < nums.size() && j < nums.size()){
            if(nums[i] == 0){ 
                if(nums[j] != 0){
                    swap(nums[i],nums[j]);
                    i++;
                }                   
            }else{
                i++;
            }
            j++;
        }
    }
};

力扣-26.刪除排序陣列中的重複項

題目連結:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() == 0)
            return 0;
        int slow = 0;
        for(int fast = 0;fast <nums.size()-1;fast++){
            if(nums[fast] != nums[fast+1])
                nums[slow++] = nums[fast];
        }
        nums[slow++] = nums[nums.size()-1];
        return slow;
    }
};

力扣-86.分隔連結串列

題目連結:https://leetcode-cn.com/problems/partition-list/

class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode* h1 = new ListNode(985);
        ListNode* h2 = new ListNode(211);
        ListNode* t1 = h1;
        ListNode* t2 = h2;
        while(head){
            if(head-> val < x){
                t1->next = head;
                t1 = t1->next;
            }else{
                t2->next = head;
                t2 = t1->next;
            }
            head = head->next;
        }
        t2->next = nullptr;
        t1->next = h2->next;
        return h1->next;
    }
};

力扣-11.盛最多水的容器

題目連結:https://leetcode-cn.com/problems/container-with-most-water/

class Solution {
public:
    int maxArea(vector<int>& height) {
        int sz = height.size();
        if(sz < 2)
            return 0;
        int maxCount = 0;
        int i = 0;
        int j = sz-1;
        while(i < j){
            int sum = min(height[i],height[j])*(j-i);
            maxCount = sum > maxCount ? sum : maxCount;
            if(height[i] < height[j])
                i++;
            else
                j--;
            
        }
        return maxCount;
    }
};

力扣-75.顏色分類

題目連結:https://leetcode-cn.com/problems/sort-colors/

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int i = 0;
        int j = nums.size()-1;
        int l = 0;
        while(i < j && l <= j){
            if(nums[l] == 2){
                swap(nums[l],nums[j]);
                j--;
            }else if(nums[l] == 0){
                swap(nums[i],nums[l]);
                i++;
                l++;
            }else{
                l++;
            }
        }
    }
};

力扣-42. 接雨水

題目連結:https://leetcode-cn.com/problems/trapping-rain-water/

class Solution {
public:
    int trap(vector<int>& height) {
        int left = 0;
        int right = height.size()-1;
        int left_max = 0;
        int right_max = 0;
        int ans = 0;
        while(left <right){
            if(height[left] < height[right]){
                if(height[left] >= left_max){
                    left_max = height[left];
                }else{
                    ans += left_max - height[left];
                }
                left++;
            }else{
                if(height[right] >= right_max){
                    right_max = height[right];
                }else{
                    ans += right_max - height[right];
                }
                right--;
            }
        }
        return ans;
    }
};

 

相關文章