1. //三数和为0的问题。要求去重,并且输出数字有序。
    public List<List<Integer>> threeSum(int[] nums)
  2. {
  3. Arrays.sort(nums);
  4. List<List<Integer>> lists = new ArrayList<List<Integer>>();
  5. //对于i去重,因为p在i后面,所以不能往后去重,可能会把p的值去掉,所以要往前去重。
  6. for(int i = 0; i < nums.length; i ++)
  7. {
  8. if(i>0&&nums[i] == nums[i-1])
  9. {
  10. continue;
  11. }
  12. int p = i+1, q = nums.length - 1;
  13. while(p < q)
  14. {
  15. int sum = nums[i]+nums[p]+nums[q];
  16. if(sum == 0)
  17. {
  18. List<Integer> list = new ArrayList<>();
  19. list.add(nums[i]);
  20. list.add(nums[p]);
  21. list.add(nums[q]);
  22. lists.add(list);
  23. //p去重很巧妙,先自加,然后判断之前p的值和自加后的值是不是相等,如果相等,再次自加,同时也避免的去重时和q重叠。
  24. while(++p < q && nums[p] == nums[p-1])
  25. {
  26. }
  27. while(--q > p && nums[q] == nums[q+1])
  28. {
  29. }
  30. }
  31. if(sum < 0)
  32. {
  33. p++;
  34. }
  35. if(sum > 0)
  36. {
  37. q--;
  38. }
  39. }
  40. }
  41. return lists;
  42. }
  1. //三数和最接近某个值
    public int threeSumClosest(int[] nums, int target) {
  2. Arrays.sort(nums);
  3. int temp = 0;
  4. int dist = Integer.MAX_VALUE;
  5. for(int i = 0; i < nums.length; i ++)
  6. {
  7. if(i > 0 && nums[i]==nums[i-1])
  8. {
  9. continue;
  10. }
  11. int p = i + 1, q = nums.length-1;
  12. while(p < q)
  13. {
  14. int sum = nums[i] + nums[p] + nums[q];
  15. if(sum > target)
  16. {
  17. if((sum - target) < dist)
  18. {
  19. dist = sum - target;
  20. temp = sum;
  21. }
  22. q--;
  23. }
  24. else if(sum < target)
  25. {
  26. if((target - sum) < dist)
  27. {
  28. dist = target - sum;
  29. temp = sum;
  30. }
  31. p++;
  32. }
  33. else
  34. {
  35. return sum;
  36. }
  37. }
  38. }
  39. return temp;
  40. }

四数和问题,感觉并不是最优解。

  1. public class Solution {
  2. public List<List<Integer>> fourSum(int[] num, int target) {
  3. Arrays.sort(num);
  4. Set<List<Integer>> hashSet = new HashSet<>();
  5. List<List<Integer>> result = new ArrayList<>();
  6.  
  7. for (int i = 0; i < num.length; i++) {
  8. for (int j = i + 1; j < num.length; j++) {
  9. int k = j + 1;
  10. int l = num.length - 1;
  11.  
  12. while (k < l) {
  13. int sum = num[i] + num[j] + num[k] + num[l];
  14.  
  15. if (sum > target) {
  16. l--;
  17. } else if (sum < target) {
  18. k++;
  19. } else if (sum == target) {
  20. ArrayList<Integer> temp = new ArrayList<Integer>();
  21. temp.add(num[i]);
  22. temp.add(num[j]);
  23. temp.add(num[k]);
  24. temp.add(num[l]);
  25.  
  26. if (!hashSet.contains(temp)) {
  27. hashSet.add(temp);
  28. result.add(temp);
  29. }
  30.  
  31. k++;
  32. l--;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. return result;
  39. }
  40. }

3Sum,4Sum问题的更多相关文章

  1. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  2. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  3. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  4. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  5. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  6. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  7. 秒杀 2Sum 3Sum 4Sum 算法题

    2 Sum 这题是 Leetcode 的第一题,相信大部分小伙伴都听过的吧. 作为一道标着 Easy 难度的题,它真的这么简单吗? 我在之前的刷题视频里说过,大家刷题一定要吃透一类题,为什么有的人题目 ...

  8. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. 1307 绳子与重物(DFS)

    1307 绳子与重物 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量 ...

  2. JS实现当鼠标移动到图片上时,时图片旋转

    <div id="container" style="width:500px;height:400px;position:relative;margin:0 aut ...

  3. SQL Server函数​

    阅读目录 SQL Server函数---Union与Union All的区别 回到顶部 SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为 ...

  4. 小程序 less wxss 混合 Mixins picker样式优化 箭头样式的实现原理

    lessc src/style/picker-arrow_.less src/style/picker-arrow_.wxss 快速入门 | Less.js 中文文档 https://less.boo ...

  5. JavaScript方法splice()和slice()

    1 splice() 1.1 说明 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目.该方法会改变原始数组.Link 1.2 语法 arrayObject.splice(inde ...

  6. setlocale()函数测试当前语言的两个程序

    http://www.cnblogs.com/cnyao/archive/2010/05/06/1729220.html setlocale()函数是用来配置地域信息的,原本以为这个也是windows ...

  7. 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类

    下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...

  8. python requests 使用

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  9. OC、C#与JAVA语法特点一些异同(差集&交集)

    C#对JAVA: 1.扩展方法 2.部分类 3.动态对象 4.匿名返回类型 5.表达式树 6.Linq 7.没有函数指针,委托,事件的直接提供方式 8.JAVA接口不规定以I开头,这个很烂的思想! J ...

  10. LATEX教程(一)

    第一个文档 打开WinEdt,建立一个新文档,将以下内容复制进入文档中,保存,保存类型选择为UTF-8. \documentclass{article} \begin{document} hello, ...