Given an array S of n integers, are there elements abc, 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, a ≤ b ≤ c ≤ d)
  • 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)

和前面的思路基本上是相同的,这里同样容易遇见相同的数,这个时候需要跳过,代码如下:

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int sz = nums.size();
vector<vector<int>> ret;
for(int i = ; i < sz; ++i){
if(i != && nums[i] == nums[i - ])
continue;
for(int j = i + ; j < sz; ++j){
if(j > i + && nums[j] == nums[j - ])
continue;
for(int beg = j + , end = sz - ; beg < end;){
while(beg > j + && nums[beg] == nums[beg - ])
beg++;
while(end < sz - && nums[end] == nums[end + ])
end--;
if(beg >= end) break;
int sum = nums[i] + nums[j] + nums[beg] + nums[end];
if(sum == target){
vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[beg]);
tmp.push_back(nums[end]);
ret.push_back(tmp);
beg++, end--;
}else if(sum < target){
beg++;
}else{
end--;
}
}
}
}
return ret;
}
};

LeetCode OJ:4Sum(4数字之和)的更多相关文章

  1. [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 ...

  2. 九度OJ 1106:数字之和 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2505 解决:1706 题目描述: 对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和. 输入: 每行输入 ...

  3. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  4. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. [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 ...

  6. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  7. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  9. LeetCode 129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...

  10. Java实现 LeetCode 129 求根到叶子节点数字之和

    129. 求根到叶子节点数字之和 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 12 ...

随机推荐

  1. LRU经典算法的原理与实现

    LRU  least recently used.顾名思义,是根据数据的活跃度进行更新的缓存算法. LRU Cache的LinkedHashMap实现: LinkedHashMap自身已经实现了顺序存 ...

  2. Android图片加载框架Picasso最全使用教程4

    通过前几篇的学习,我们已经对Picasso的加载图片的用法有了很深的了解,接下来我们开始分析Picasso为我们提供的其他高级功能及内存分析,Let’sGo ! Picasso进行图片的旋转(Rota ...

  3. 微信小程序组件checkbox

    表单组件checkbox:官方文档 Demo Code: JS Page({ data:{ items:[ {name: 'USA', value: '美国'}, {name: 'CHN', valu ...

  4. 129. Sum Root to Leaf Numbers(从根节点加到叶子节点的和)

      Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb ...

  5. 62二叉搜索树的第k个结点

    题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 思路 二叉搜索树的中序遍历的输出结果是拍好序 ...

  6. HDOJ 1238 Substrings 【最长公共子串】

    HDOJ 1238 Substrings [最长公共子串] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. windows下的DeepLearning环境搭建:Theano的安装

    我的系统版本:windows8.1 64位 安装theano需要安装python.numpy等很多东西,为了简便,我这里用的是Anaconda 首先,清理电脑上的所有有关python的组件(可不清理, ...

  8. Leaflet API 翻译(二)

    摘自:http://www.ithao123.cn/content-824673.html L.Point 显示以像素为单位的点的x,y坐标. 所以接受点对象的leaflet方法和选项都也接受他们简单 ...

  9. wyx20162314实验报告二

    北京电子科技学院BESTI实验报告 课程:程序设计与数据结构 班级: 1623 姓名: 王译潇 学号:20162310 指导教师:娄佳鹏老师.王志强老师 实验日期:2017年3月26号 实验密级: 非 ...

  10. 解决Vim插入模式下backspace按键无法删除字符的问题【转】

    本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...