LeetCode题目总结:双指针技巧||题目汇总

目录

题目列表:

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

633.平方数之和

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

680.验证回文串Ⅱ

88.合并两个有序数组

141.环形链表

524.通过删除字母匹配到字典里的最长单词


以下内容参考guthub:https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20题解%20-%20双指针.md

leetcode上的题目大多数都是面试的高频题,事实上,我们不可能记住每个题目,因此,将相同类型的题目加以总结是最有效的办法。

以下几个题目都是利用双指针的技巧来进行解决,题目难度从浅到深,因此,我仅给出代码。

题目列表:

  1. 两数之和Ⅱ-输入有序数组
  2. 平方数之和
  3. 反转字符串中的元音字母
  4. 验证回文字符串Ⅱ
  5. 合并两个有序数组
  6. 环形链表
  7. 通过删除字母匹配到字典里的最长单词

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

LeetCode题目总结:双指针技巧||题目汇总_第1张图片

class Solution {
public:
    vector twoSum(vector& numbers, int target) {
        int index1 = 0;
        int index2 = numbers.size()-1;
        vector result;
        while(index1

633.平方数之和

LeetCode题目总结:双指针技巧||题目汇总_第2张图片

class Solution {
public:
    bool judgeSquareSum(int c) {
        if(c<=1)
            return true;
        long i =0;
        long j =sqrt(c);
        while(i<=j)
        {
            if( i*i+j*j==c)
                return true;
            if( i*i+j*j

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

LeetCode题目总结:双指针技巧||题目汇总_第3张图片

class Solution {
public:
    string reverseVowels(string s) {
        if(s.empty()) return s;
        int l=0,r=s.size()-1;
        while(l<=r){
            if(check(s[l])&&!check(s[r])){
                r--;
            }
            else if(!check(s[l])&&check(s[r])){
                l++;
            }
            else if(!check(s[l])&&!check(s[r]))
            {
                r--;l++;
            }
            
            else if(check(s[l])&&check(s[r])) 
            {
                swap(s[l],s[r]);
                l++;
                r--;
            }
        }
        return s;
    }

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

680.验证回文串Ⅱ

LeetCode题目总结:双指针技巧||题目汇总_第4张图片

class Solution {
public:
    bool validPalindrome(string s) {
        if(s.length()<=1)
            return true;
        int b = 0,e = s.length()-1;
        while(b<=e)
        {
            if(s[b]!=s[e])
                return Ispadindromes(s,b+1,e)||Ispadindromes(s,b,e-1);
            b++,e--;
        }
        return true;
    }
    bool Ispadindromes(string s,int begin, int end)
    {
        while(begin<=end)
        {
            if(s[begin]!=s[end])
              return false;
            ++begin,--end;
        }
        return true;
    }
};

88.合并两个有序数组

LeetCode题目总结:双指针技巧||题目汇总_第5张图片

class Solution {
public:
    void merge(vector& nums1, int m, vector& nums2, int n) {
        //思路:由于numsq中有足够大的空间,因此可以从后往前进行一个归并,
        //这样在归并的过程中就不会覆盖掉nums1中的数字
        int index1 = m-1;
        int index2 = n-1;
        int len = m+n-1;
        while(index1>=0||index2>=0)
        {
            if(index1<0)
            {
                nums1[len--] = nums2[index2--];
                continue;
            }
            if(index2<0)
            {
                nums1[len--] = nums1[index1--];
                continue;
            }
            if(nums1[index1]

141.环形链表

LeetCode题目总结:双指针技巧||题目汇总_第6张图片

/**
 * 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)
            return false;
        ListNode *fast = head;
        ListNode *low = head;
        while(fast!=NULL&&fast->next!=NULL)
        {
            fast=fast->next->next;
            low = low->next;
            if(fast==low)
                return true;
        }
        return false;
    }
};

524.通过删除字母匹配到字典里的最长单词

LeetCode题目总结:双指针技巧||题目汇总_第7张图片

class Solution {
public:
    string findLongestWord(string s, vector& d) {
        string reslut="";
        for(auto e:d)
        {
            int l1 = reslut.length(),l2 = e.length();
            if(l1>l2||(l1==l2&&reslut.compare(e)<0))
            {
                continue;
            }
            if(isSubStr(s,e))
                reslut = e;
        }
        return reslut;

    };
    bool isSubStr(string str,string target)
    {
        //用双指针判断target是否为str的子串
        int i = 0,j = 0;
        while(i

 

你可能感兴趣的:(刷题)