双指针法LeetCode总结

双指针方法

遍历对象的过程中,使用两个指针进行操作,实现相应的目的

快慢指针

经典环形链表

[LeetCode142]

待补充

对撞指针

适用于有序数组,设置数组左索引与数组右索引

代码基本流程是

public f(int[] nums){
    int left = 0;
    int right = nums.length-1;
    while (left <= right){
        left++;
        //进行操作
        rigth--;
    }
}

[LeetCode11]

盛水最多的容器

class Solution {
    public static int maxArea(int[] height) {
        int max = 0,tmp =0;
        int left = 0,right = height.length-1;
        while (right>left){
            //计算当前两指针内最大值 然后进行指针移动操作 这里应该是当时看的题解。。。
            tmp = height[right]>height[left]? height[left++]*(right-left+1):height[right--]*(right-left+1);
            max = max>tmp? max:tmp;
        }
        return max;
    }
}

[LeetCode15]

三数之和

早期刷的题基本都是看的题解。。。哭了

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        //构造有序数组
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        //从头开始遍历
        for (int first = 0; first < n; ++first) {
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            //从当前元素以后设置对撞指针
            int third = n - 1;
            int target = -nums[first];
            //left++在for循环里面
            for (int second = first + 1; second < n; ++second) {
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                //rigth--
                while (second < third && nums[second] + nums[third] > target) {
                    --third;
                }
                //对撞返回
                if (second == third) {
                    break;
                }
                //找到结果以后存储
                if (nums[second] + nums[third] == target) {
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    ans.add(list);
                }
            }
        }
        return ans;
    }
}

[LeetCode16]

最接近的三数之和

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);

        int lens = nums.length;

        int answer = nums[0] + nums[1] +nums[2];


        for (int first = 0; first < lens-2; first++) {

            if (first > 0 && nums[first] == nums[first-1]){
                continue;
            }

            int second = first+1, third = lens-1;
            while (second < third){
                int sum  = nums[first] + nums[second] +nums [third];
                if (Math.abs(target - sum) < Math.abs(target - answer))
                    answer = sum;
                //有序数组的核心在于减少指针的移动次数 我这里本来设置的太sb了 差点搞成暴力
                if (sum >target) third --;
                else if (sum <target) second++;
                else return answer;
            }
        }
        return answer;
    }
}

[LeetCode42]

接雨水 待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值