018、NSUM问题(labuladong)

文章介绍了LeetCode中与数组求和相关的经典问题,包括两数之和、三数之和、两数之和II(输入有序数组)以及四数之和的解决方案。主要使用了排序、哈希表和双指针等算法思想来解决这些问题。

NSUM问题

1、两数之和

力扣第1题,两数之和

[1]两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        // 无序就使用hash表
        // key:元素 value:元素的位置
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            // 加入元素到hashMap前先判断
            if (map.containsKey(temp)) {
                return new int[]{map.get(temp), i};
            }
            map.put(nums[i], i);
        }
        return new int[]{-1, -1};
    }
}

2、三数之和

力扣第15题,三数之和

[15]三数之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new LinkedList<>();
        // 先将数组从小到大排序
        Arrays.sort(nums);
        // 每次选好
        for (int i = 0; i < nums.length - 2; i++) {
            // 条件判断
            if (nums[i] > 0) {
                return result;
            }
            // 防止重复的元素加入到结果集
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1, right = nums.length - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    // 去重
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    left++;
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    right--;
                } else if (sum < 0) {
                    left++;
                } else if (sum > 0) {
                    right--;
                }
            }
        }

        return result;
    }
}

3、两数之和Ⅱ-输入有序数组

力扣第167题,两数之和Ⅱ-输入有序数组

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

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // 遇见排序就使用左右指针
        for (int left = 0; left < numbers.length - 1; left++) {
            // 跳过重复元素
            if (left > 0 && numbers[left] == numbers[left - 1]) {
                continue;
            }
            int right = numbers.length - 1;
            while (left < right) {
                // 结果比较
                int sum = numbers[left] + numbers[right];
                if (sum == target) {
                    return new int[]{left + 1, right + 1};
                } else if (sum < target) {
                    left++;
                } else {
                    right--;
                }
            }
        }
        // 找不到
        return new int[]{-1, -1};
    }
}

4、四数之和

力扣第18题,四数之和

[18]四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new LinkedList<>();
        // 先将数组进行排序
        Arrays.sort(nums);
        // 排序后就可以用指针开始遍历
        for (int i = 0; i < nums.length - 3; i++) {
            // 去重
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length - 2; j++) {
                // 去重
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int left = j + 1, right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        // 去重
                        while (left < right && nums[left] == nums[++left]) ;
                        while (left < right && nums[right] == nums[--right]) ;
                    } else if (sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }

        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值