问题描述:

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

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • The solution set must not contain duplicate quadruplets.
    For example, given array S = {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)

解题思路:

与之前的3sum相类似,只不过在外边多了一套循环,时间复杂度为O(n^3)

代码如下:

public class Solution {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> fourSum(int[] nums, int target) {
int length = nums.length;
int tmp;
if (nums == null || length < 4)
return ans;
Arrays.sort(nums);
for (int i = 0; i < length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
int begin = j + 1;
int end = length - 1;
while (begin < end) {
tmp = nums[begin] + nums[end] + nums[i] + nums[j];
if (tmp == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[begin]);
list.add(nums[end]);
ans.add(list);
while (begin < end && nums[begin + 1] == nums[begin])
begin++;
begin++;
while (begin < end && nums[end - 1] == nums[end])
end--;
end--;
} else if (tmp > target)
end--;
else
begin++;
}
}
}
return ans;
}
}

Java [leetcode 18]4Sum的更多相关文章

  1. leetcode 18 4Sum JAVA

    题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...

  2. [LeetCode] 18. 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 ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. LeetCode 18. 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 ...

  5. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  6. [LeetCode] 18. 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 ...

  7. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  8. Leetcode 18. 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. C#解leetcode 18. 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 ...

随机推荐

  1. html 页面 ajax 方法显示遮罩

    showLoading.css 样式: ;;list-style-type:none;} a,img{;} .overlay{;;;;;width:100%;height:100%;_padding: ...

  2. EXTJS 4.2 资料 控件之tabpanel 静态生成tabpanel

    //**************页面主体开始***************** var tabpanel = Ext.createWidget('tabpanel', { activeTab: 0, ...

  3. objective-C 中两种实现动画的方法(转)

     转发自:http://wayne173.iteye.com/blog/1250232 第一种方法: [UIView beginAnimations:@"Curl"context: ...

  4. .net google calendar

    https://developers.google.com/gdata/client-cs http://www.codeproject.com/Articles/64474/How-to-Read- ...

  5. JS创建类和对象

    JavaScript 创建类/对象的几种方式 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活 ...

  6. MVC EF异常-“序列化类型为 XX 的对象时检测到循环引用”

    原因:在EF实体中,两个互为主外键关系的实体类的导航属性相互引用. 解决方法一:删除一个不需要的类的导航属性 方法二:使用DTO模型 方法三:直接返回需要的属性(不能包括相互引用的属性)

  7. Firefly安装说明 与 常见问题

    原地址:http://bbs.gameres.com/thread_223688.html 第三方库依赖:    twisted, python-memcached ftp://ftp.tummy.c ...

  8. PHP的执行原理/执行流程

    http://www.cnblogs.com/hongfei/archive/2012/06/12/2547119.html 更深入的学习和了解可以查看下面: 风雨的博客http://www.laru ...

  9. photoshop:把路径存储为形状

    这个其实跟定义画笔步骤是一样的 路径存储为自定义形状 1.用路径选择工具(快捷键A),选中路径 2.菜单:编辑->定义自定形状 3.选择自定义形状工具(快捷键U),可以看到刚定义的形状 把当前形 ...

  10. WPF中动态添加xaml资源文件

    一.新建一个资源文件,然后设置其Build Actoin(生成操作)为Resource(资源): 二.在App.xaml.cs的StartUp事件或者是你需要的时机代码段写上如下代码: Resourc ...