算法基础:双指针相关题目

删除链表中相同元素

public ListNode deleteDuplicates(ListNode head) {
        ListNode res = new ListNode(0, head);
        ListNode cur = res;
        while (cur != null && cur.next.next != null) {
            if (cur.next.val == cur.next.next.val) {
                int val = cur.next.val;
                while (cur.next != null && cur.next.val == val) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }
        return res.next;
    }

该题目需要进行至少连续两个节点值的判断,那么可以在原链表基础上做一个空头节点,从该空头结点就可以将整个链表从头开始都两个两个判断(cur.next和cur.next.next),当下两个值相等,不能直接跳过,需要再次基础上再向后延伸,知道跳过所有相同节点。

三数之和

记录数组中三个数组合和为0的情况。

public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 2; i++) {
            if (nums[i] > 0) {
                break;
            }
            if(i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int head = i + 1;
            int tail = nums.length - 1;
            while (head < tail) {
                int temp = nums[i] + nums[head] + nums[tail];
                if (temp == 0) {
                    res.add(new ArrayList<>(Arrays.asList(nums[i], nums[head], nums[tail])));
                    while (head < tail && nums[head] == nums[++head]);
                    while (head < tail && nums[tail] == nums[--tail]);
                } else if (temp < 0) {
                    while (head < tail && nums[head] == nums[++head]);
                } else {
                    while (head < tail && nums[tail] == nums[--tail]);
                }
            }
        }
        return res;
    }

思路是对排序后的数组,以i下标为第一个数基准,再分别以i+1和末尾,这三个值进行和值判断。因为整体升序,处理情况分以下三种:
①和大于0:说明正数过大,将尾向左移即可
②和小于0:说明负数过小,将头向右移即可
③和等于0:记录当前值,将头尾都移动。
但i基准值移动时,需要进行与i-1值相同与否的判断,因为排序后,两个相同值进行判断结果是相同的,会出现重复,需要将相同基准值跳过。

优化:
①在上述基础上,每次头尾移动时,若值相同则没有重新判断的必要,所以移动时跳过所有相同值可以加速过程。
②当第一个基准值大于零时,因为处于排序数组中,其后续值全是正值,相加不可能为0,所以当基准值为正值,直接结束遍历循环即可。

比较含退格的字符串

s = “ab#c”, t = "ad#c"给出如下字符串,#会将其前面的字符删掉。

public boolean backspaceCompare(String s, String t) {
        return format(s).equals(format(t));
    }
    
    public String format(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        int back = 0;
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str.charAt(i) == '#') {
                back++;
            } else if (back > 0) {
                back--;
            } else if (back == 0){
                stringBuilder.append(str.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

实际就是两个指针分别遍历两个字符串,从后往前,记录遇到的#个数并且伴随指针移动进行字符删减,将结果保存起来。

区间列表的交集

public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        ArrayList<int[]> res = new ArrayList<>();
        int i = 0;
        int j = 0;
        while (i != firstList.length && j != secondList.length) {
            int head = Math.max(firstList[i][0], secondList[j][0]);
            int tail = Math.min(firstList[i][1], secondList[j][1]);
            if (head <= tail) {
                res.add(new int[] {head, tail});
            }
            if (firstList[i][1] < secondList[j][1]) {
                i++;
            } else {
                j++;
            }
        }
        return res.toArray(new int[res.size()][]);
    }

两个指针分别遍历两个记录数组,重叠部分判断:
重叠开头部分:判断两个数组头部(即下标0),较大的即为重叠部分头部
重叠结尾部分:判断两个数组尾部(即下标1),较小的即为重叠部分尾部
若头<=尾,记录该结果
移动指针条件:
当区间A包含完区间B,那么区间B需要进到下一个区间;故判断点在于包含关系,即尾部的大小关系:
若区间A尾<区间B尾,则区间A需要进下一个区间,即i++;反之j++

盛最多水的容器

头尾两部分,求其中能盛水的容量。

public int maxArea(int[] height) {
        int head = 0;
        int tail = height.length - 1;
        int res = 0;
        while (head < tail) {
            res = height[head] < height[tail] ?
                    Math.max((tail - head) * height[head++], res):
                    Math.max((tail - head) * height[tail--], res);
        }
        return res;
    }

首先求出容量大小表达式:头尾较低的*头尾距离
所以使用两个指针分别头尾遍历,先判断头尾哪个更低,带入求面积,和先前存储的面积大小判断求较大值。
下标移动:需要将更低的那一端移动,因为当前较低,只有移动才有可能增高,使容量升高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔幻音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值