📖
Data Structure
  • 基本概念
  • 线性表
    • 顺序表
    • 链表
    • 广义表
  • 栈
  • 队列
  • 字符串
  • 树
    • 二叉树
    • 二叉搜索树
    • 平衡二叉树
    • 堆
    • 哈夫曼树
  • 图
    • DFS
    • BFS
  • 排序
    • 选择排序
    • 插入排序
    • 比较选择排序和插入排序
    • 希尔排序
  • 常用算法
    • 排序
    • 二叉树遍历
    • 根据两种遍历顺序重构树
    • 二叉树深度
    • 最近公共祖先
    • 回溯集合
    • N Sum
    • union-find
  • 常用算法时间复杂度速查表
由 GitBook 提供支持
在本页
  • 1. Two Sum
  • 2. 3Sum
  • 3. 4Sum
  • 4. nSum

这有帮助吗?

  1. 常用算法

N Sum

上一页回溯集合下一页union-find

最后更新于4年前

这有帮助吗?

1. Two Sum

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        if (map.containsKey(target - nums[i])) {
            return new int[]{map.get(target - nums[i]), i};
        }
        map.put(nums[i], i);
    }
    return null;
}
public List<int[]> twoSum(int[] nums, int target) {

    List<int[]> res = new ArrayList<>();

    Arrays.sort(nums);

    int lo = 0, hi = nums.length - 1;
    while (lo < hi) {
        int sum = nums[lo] + nums[hi];
        int left = nums[lo], right = nums[hi];
        if (sum < target) {
            while (lo < hi && nums[lo] == left) lo++;
        } else if (sum > target) {
            while (lo < hi && nums[hi] == right) hi--;
        } else {
            res.add(new int[]{left, right});
            while (lo < hi && nums[lo] == left) lo++;
            while (lo < hi && nums[hi] == right) hi--;
        }
    }
    return res;

2. 3Sum

public List<List<Integer>> threeSum(int[] nums) {

    List<List<Integer>> res = new ArrayList<>();

    Arrays.sort(nums);
    for (int i = 0; i < nums.length; i++) {
        List<List<Integer>> temp = twoSum(nums, i + 1, 0 - nums[i]);
        for (List<Integer> t : temp) {
            t.add(0, nums[i]);
            res.add(t);
        }

        while (i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
    }

    return res;

}

private List<List<Integer>> twoSum(int[] nums, int start, int target) {

    List<List<Integer>> res = new ArrayList<>();

    int lo = start, hi = nums.length - 1;
    while (lo < hi) {
        int sum = nums[lo] + nums[hi];
        int left = nums[lo], right = nums[hi];
        if (sum < target) {
            while (lo < hi && nums[lo] == left) lo++;
        } else if (sum > target) {
            while (lo < hi && nums[hi] == right) hi--;
        } else {
            List<Integer> temp = new ArrayList<>();
            temp.add(left);
            temp.add(right);
            res.add(temp);
            while (lo < hi && nums[lo] == left) lo++;
            while (lo < hi && nums[hi] == right) hi--;
        }
    }
    return res;
}

3. 4Sum

public List<List<Integer>> fourSum(int[] nums, int target) {

    List<List<Integer>> res = new ArrayList<>();

    Arrays.sort(nums);

    for (int i = 0; i < nums.length; i++) {
        List<List<Integer>> temp = threeSum(nums, i + 1, target - nums[i]);
        for (List<Integer> t : temp) {
            t.add(0, nums[i]);
            res.add(t);
        }

        while (i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
    }

    return res;

}

private List<List<Integer>> threeSum(int[] nums, int start, int target) {

    List<List<Integer>> res = new ArrayList<>();

    for (int i = start; i < nums.length; i++) {
        List<List<Integer>> temp = twoSum(nums, i + 1, target - nums[i]);
        for (List<Integer> t : temp) {
            t.add(0, nums[i]);
            res.add(t);
        }

        while (i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
    }

    return res;

}

private List<List<Integer>> twoSum(int[] nums, int start, int target) {

    List<List<Integer>> res = new ArrayList<>();

    int lo = start, hi = nums.length - 1;
    while (lo < hi) {
        int sum = nums[lo] + nums[hi];
        int left = nums[lo], right = nums[hi];
        if (sum < target) {
            while (lo < hi && nums[lo] == left) lo++;
        } else if (sum > target) {
            while (lo < hi && nums[hi] == right) hi--;
        } else {
            List<Integer> temp = new ArrayList<>();
            temp.add(left);
            temp.add(right);
            res.add(temp);
            while (lo < hi && nums[lo] == left) lo++;
            while (lo < hi && nums[hi] == right) hi--;
        }
    }
    return res;
}

4. nSum

private List<List<Integer>> nSum(int[] nums, int n, int start, int target) {
    int size = nums.length;
    List<List<Integer>> res = new ArrayList<>();

    if (n < 2 || size < n) return res;
    if (n == 2) {
        int lo = start, hi = size - 1;
        while (lo < hi) {
            int sum = nums[lo] + nums[hi];
            int left = nums[lo], right = nums[hi];
            if (sum < target) {
                while (lo < hi && nums[lo] == left) lo++;
            } else if (sum > target) {
                while (lo < hi && nums[hi] == right) hi--;
            } else {
                List<Integer> temp = new ArrayList<>();
                temp.add(left);
                temp.add(right);
                res.add(temp);
                while (lo < hi && nums[lo] == left) lo++;
                while (lo < hi && nums[hi] == right) hi--;
            }
        }
    } else {
        for (int i = start; i < size; i++) {
            List<List<Integer>> sub = nSum(nums, n - 1, i + 1, target - nums[i]);
            for (List<Integer> t : sub) {
                t.add(0, nums[i]);
                res.add(t);
            }
            while (i < size - 1 && nums[i] == nums[i + 1]) i++;
        }
    }
    return res;
}
LogoLoading...LeetCode
LogoLoading...LeetCode
LogoLoading...LeetCode