LeetCode 018 4Sum
题目描述:4Sum
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, 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> &num, int target) {
// Note: The Solution object is instantiated only once.
vector<vector<int>> res;
int numlen = num.size(); if(num.size()<4)
return res; sort(num.begin(),num.end()); set<vector<int>> tmpres; for(int i = 0; i < numlen; i++){
for(int j = i + 1; j < numlen; j++){ int begin = j+1;
int end = numlen-1; while(begin < end){
int sum = num[i]+ num[j] + num[begin] + num[end];
if(sum == target){
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[begin]);
tmp.push_back(num[end]);
tmpres.insert(tmp);
begin++;
end--;
}
else if(sum<target) begin++;
else end--;
}
}
} set<vector<int>>::iterator it = tmpres.begin();
for(; it != tmpres.end(); it++)
res.push_back(*it);
return res;
}
};
LeetCode 018 4Sum的更多相关文章
- 【JAVA、C++】LeetCode 018 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 ...
- [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 ...
- 【LeetCode】018 4Sum
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [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 ...
- 018 4Sum 四个数的和
给定一个含有 n 个整数的数组 S,数列 S 中是否存在元素 a,b,c 和 d 使 a + b + c + d = target ?请在数组中找出所有满足各元素相加等于特定值的不重复组合.注意:解决 ...
- [LeetCode] 454. 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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 ...
随机推荐
- APIview + Serializers
1.APIview使用 https://www.cnblogs.com/xiaonq/p/10124104.html https://www.cnblogs.com/xiaonq/p/109878 ...
- SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂)
SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂) 当前环境说明: Windows10_64 Maven3.x JDK1.8 MySQL5.6 SpringTool ...
- 转载:解密Redis持久化
本文内容来源于Redis作者博文,Redis作者说,他看到的所有针对Redis的讨论中,对Redis持久化的误解是最大的,于是他写了一篇长文来对Redis的持久化进行了系统性的论述.文章非常长,也很值 ...
- GAMES101系列笔记一 图形学概述与线性代数入门
概述+线性代数 为什么学习图形学? Computer Graphics is AWESOME! 主要涉及内容: 光栅化 曲线和网格 光线追踪 动画与模拟 Differences between CG ...
- 【译】关于Rust模块的清晰解释
原文链接: http://www.sheshbabu.com/posts/rust-module-system/ 原文标题: Clear explanation of Rust's module sy ...
- 09线程隔离的g对象
1,g是global的意思. g对象再一次请求中的所有的代码的地方,都是可以使用的. 同一次请求,那么在这个项目的所有地方都可以用了. from flask import Flask,request, ...
- linux netfilter rule match target 数据结构
对于netfilter 可以参考 https://netfilter.org/documentation/HOWTO/netfilter-hacking-HOWTO-3.html netfilter ...
- 七:Redis的持久化
1.RDB(Redis DataBase) 1.1 定义:在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的snapshot快照,他恢复时是将快照文件直接读到内存里 是什么:Redis会单 ...
- [原题复现]SUCTF 2019 WEB EasySQL(堆叠注入)
简介 原题复现: 1 <?php 2 session_start(); 3 4 include_once "config.php"; 5 6 $post = array() ...
- 思维导图MindManager有新手引导功能吗
无论是对于初次使用Mindmanager思维导图软件的新手来说,还是对于有一定软件使用基础的进阶者来说,Mindmanager思维导图软件的帮助功能都能给予用户很大的指导作用. Mindmanager ...