刷题之数组题——双指针

刷题———数组
问题一:11、盛更多的水的容器(数组中两个数中间的面积)
解法:双指针

class Solution {

    public int maxArea(int[] height) {

        int i = 0, j = height.length - 1, res = 0;

        while(i < j) {

            res = height[i] < height[j] ? 

                Math.max(res, (j - i) * height[i++]): 

                Math.max(res, (j - i) * height[j--]); 

        }

        return res;

    }

}

自己动手易错点:Java语言格式不了解,数组长度为A.length;A=i<j?true:false;
问题二:15 三数之和(输出数组中之和为0 的三个数)
解法:排序加双指针

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        // 枚举 a
        for (int first = 0; first < n; ++first) {
            // 需要和上一次枚举的数不相同
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            // c 对应的指针初始指向数组的最右端
            int third = n - 1;
            int target = -nums[first];
            // 枚举 b
            for (int second = first + 1; second < n; ++second) {
                // 需要和上一次枚举的数不相同
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                // 需要保证 b 的指针在 c 的指针的左侧
                while (second < third && nums[second] + nums[third] > target) {
                    --third;
                }
                // 如果指针重合,随着 b 后续的增加
                // 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
                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;
    }
}

**自己动手易错点:**基础知识中:排序Arrays.sort(A);列表基本操作:定义List<Integer/String> A = new ArrayList<Integer/String>();新增A.add(1/“1”)其他基本操作参考
三指针:要考虑break的情况(如本题中的third在second的循环中)
优化算法

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        int n = nums.length;
        for (int i = 0; i < n - 2; ++i) {
            int x = nums[i];
            if (i > 0 && x == nums[i - 1]) continue; // 跳过重复数字
            if (x + nums[i + 1] + nums[i + 2] > 0) break; // 优化一
            if (x + nums[n - 2] + nums[n - 1] < 0) continue; // 优化二
            int j = i + 1, k = n - 1;
            while (j < k) {
                int s = x + nums[j] + nums[k];
                if (s > 0) --k;
                else if (s < 0) ++j;
                else {
                    ans.add(List.of(x, nums[j], nums[k]));
                    for (++j; j < k && nums[j] == nums[j - 1]; ++j); // 跳过重复数字
                    for (--k; k > j && nums[k] == nums[k + 1]; --k); // 跳过重复数字
                }
            }
        }
        return ans;
    }
}

优化的想法首先考虑排序后first后面的数组存不存在这三个元素,再用除去first的两个元素使用双指针;ans.add(List.of(A,B,C));ans.add(Arrays.asList(A,B,C));
问题三:16、最接近的三数之和
解法和上题一致,用打擂台的方式比出和结果最接近的值。绝对值:Math.abs(A)

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int ans = nums[0] + nums[1] + nums[2];
        Arrays.sort(nums);
        int n = nums.length - 1;
        for(int first  = 0; first < n-1; first++){
            if(first > 0 && nums[first] == nums[first - 1]){
                continue;
            } 
            int second = first + 1, third = n;
            int target1 = target - nums[first];
            while(second < third){
                int sum = nums[second] + nums[third];
                 if(sum == target1) return target;
                 if(Math.abs(sum + nums[first] - target) < Math.abs(ans - target))
                    ans = sum + nums[first];
                 if(sum < target1){
                    second++;
                }
                if(sum > target1){
                    third--;
                }           
            }
        }
        return ans;
    }
}

自己动手易错点:first最后一个是nums.length-2;最后输出的是最接近的数,不用判断是否存在三个正好的数;可以先打擂台再比较和双指针的移位;可以用for(a++;a<b&&A;a++)的方式双指针排除重复的移位。
问题四:18、四数之和
解法:双循环加双指针
双循环中要跳过重复加判断是否存在;双指针中有相等放入答案+移位+去重;不同则移位;输入不存在如下:

 if (nums == null || nums.length < 4) {
            return ans;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值