• LeetCode_18 4Sum


    Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

    Note:

    The solution set must not contain duplicate quadruplets.

    Example:

    Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
    A solution set is:
    [
      [-1,  0, 0, 1],
      [-2, -1, 1, 2],
      [-2,  0, 0, 2]
    ]
     1 public List<List<Integer>> fourSum(int[] nums, int target) {
     2         List<List<Integer>> res = new ArrayList<>();
     3         Arrays.sort(nums);
     4         for (int i = 0; i < nums.length - 3; i++) {
     5             if (i != 0 && nums[i] == nums[i - 1])
     6                 continue;
     7             for (int j = i + 1; j < nums.length - 2; j++) {
     8                 if (j > i + 1 && nums[j] == nums[j - 1])
     9                     continue;
    10                 int k = j + 1;
    11                 int l = nums.length - 1;
    12                 while (k < l) {
    13                     int sum = nums[i] + nums[j] + nums[k] + nums[l];
    14                     if (sum == target) {
    15                         List<Integer> list = new ArrayList<>();
    16                         list.add(nums[i]);
    17                         list.add(nums[j]);
    18                         list.add(nums[k]);
    19                         list.add(nums[l]);
    20                         res.add(list);
    21                         k++;
    22                         l--;
    23                         // 去重复
    24                         while (k < l && nums[k] == nums[k - 1]) {
    25                             k++;
    26                         }
    27                         while (k < l && nums[l] == nums[l + 1]) {
    28                             l--;
    29                         }
    30                     } else if (sum < target) {
    31                         k++;
    32                     } else {
    33                         l--;
    34                     }
    35                 }
    36             }
    37         }
    38         return res;
    39     }
     1 public List<List<Integer>> fourSum3(int[] num, int target) {
     2         ArrayList<List<Integer>> ans = new ArrayList<>();
     3         if (num.length < 4)
     4             return ans;
     5         Arrays.sort(num);
     6         for (int i = 0; i < num.length - 3; i++) {
     7             if (num[i] + num[i + 1] + num[i + 2] + num[i + 3] > target)
     8                 break; // first candidate too large, search finished
     9             if (num[i] + num[num.length - 1] + num[num.length - 2] + num[num.length - 3] < target)
    10                 continue; // first candidate too small
    11             if (i > 0 && num[i] == num[i - 1])
    12                 continue; // prevents duplicate result in ans list
    13             for (int j = i + 1; j < num.length - 2; j++) {
    14                 if (num[i] + num[j] + num[j + 1] + num[j + 2] > target)
    15                     break; // second candidate too large
    16                 if (num[i] + num[j] + num[num.length - 1] + num[num.length - 2] < target)
    17                     continue; // second candidate too small
    18                 if (j > i + 1 && num[j] == num[j - 1])
    19                     continue; // prevents duplicate results in ans list
    20                 int low = j + 1, high = num.length - 1;
    21                 while (low < high) {
    22                     int sum = num[i] + num[j] + num[low] + num[high];
    23                     if (sum == target) {
    24                         ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
    25                         while (low < high && num[low] == num[low + 1])
    26                             low++; // skipping over duplicate on low
    27                         while (low < high && num[high] == num[high - 1])
    28                             high--; // skipping over duplicate on high
    29                         low++;
    30                         high--;
    31                     }
    32                     // move window
    33                     else if (sum < target)
    34                         low++;
    35                     else
    36                         high--;
    37                 }
    38             }
    39         }
    40         return ans;
    41     }
  • 相关阅读:
    PHP中利用jQuery操作json格式数据,实现$_POST的数据传输和接收
    如何快速掌握一门技术【婴儿最强学习回头看一看】
    显示桌面.scf
    注册表数据库
    win10home_fixgpedit.msc
    Eclipse 中 jetty 调试模式(debug)正常启动无法访问;非调试模式正常
    svn中的与资源库同步操作 讲解
    windows下二进制mysql的卸载以及安装教程
    mysql服务正在启动或停止中请稍后片刻再试一次,服务强制杀死的方法
    Eclipse中git检出、更新、提交、合并分支、以及解决冲突
  • 原文地址:https://www.cnblogs.com/ntbww93/p/9097052.html
Copyright © 2020-2023  润新知