【基础算法-双指针】刷过这些,你才能说会双指针了

左右指针 ->

11. 盛最多水的容器(左右指针)

class Solution {
    
    
public:
    int maxArea(vector<int>& height) {
    
    
        int res = 0;
        int l = 0,r = height.size() - 1;
        while(l < r)
        {
    
    
            res = max(res, min(height[l],height[r]) * (r - l));
            if(height[l] <= height[r]) l ++ ;
            else r -- ;
        }
        return res;
    }
};

167. 两数之和 II - 输入有序数组

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

345. 反转字符串中的元音字母

class Solution {
    
    
public:

    bool check(char c)
    {
    
    
        if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') return true;
        return false;
    }

    string reverseVowels(string s) {
    
    
        int i = 0, j = s.size() - 1;
        while(i < j)
        {
    
    
            while(i < s.size() && !check(s[i])) i ++ ;
            while(j >= 0 && !check(s[j])) j -- ;
            if(i < j) swap(s[i ++ ],s[j --]);
        }
        return s;
    }
};

快慢指针 ->

876. 链表的中间结点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* middleNode(ListNode* head) {
    
    
        auto fast = head, slow = head;
        while(fast->next)
        {
    
    
            fast = fast->next;
            if(fast->next) fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
};

141. 环形链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    bool hasCycle(ListNode *head) {
    
    
        if(head == NULL || head->next == NULL) return false;

        auto fast = head->next, slow = head;
        while(slow != fast)
        {
    
    
            if(fast == NULL || fast->next == NULL) return false;
            slow = slow->next;
            fast = fast->next->next;
        }
        return true;
    }
};

剑指 Offer 22. 链表中倒数第k个节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
    
    
        auto fast = head,slow = head;
        while(k -- ) fast = fast->next;
        while(fast)
        {
    
    
            fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
};

参考资料

猜你喜欢

转载自blog.csdn.net/weixin_43154149/article/details/112863001