18. 4Sum
#include <string>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
int len = nums.size();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
int third = j + 1, fouth = len - 1;
while (third < fouth) {
int sum = nums[i]+nums[j]+ nums[third]+nums[fouth];
if (target == sum) {
vector<int> temp = { nums[i],nums[j],nums[third],nums[fouth] };
res.push_back(temp);
while (third < fouth && nums[third ] == temp[2]) third++;
while (third < fouth && nums[fouth ] == temp[3]) fouth--;
}
else sum < target ? third++ : fouth--;
}
while ((j + 1 < len) && nums[j] == nums[j + 1]) j++;
}
while ((i + 1 < len) && nums[i] == nums[i + 1]) i++;
}
return res;
}
int main() {
vector<int> nums = { 1,0,-1,0,-2,2};
int target = 0;
vector<vector<int>> res=fourSum(nums, target);
}
18. 4Sum的更多相关文章
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + 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 ...
- 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 ...
- 18. 4Sum (JAVA)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 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 ...
随机推荐
- SETLOCAL
Quote from: http://ss64.com/nt/setlocal.html SETLOCAL Set options to control the visibility of envir ...
- Lua数组排序
代码 network = { {name = "grauna", IP = "210.26.30.34"}, {name = "arraial&quo ...
- Range Minimum Query and Lowest Common Ancestor
作者:danielp 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAnc ...
- nginx 安装过程中遇到的问题
安装Nginx时报错 ./configure: error: the HTTP rewrite module requires the PCRE library. 安装pcre-devel解决问题yu ...
- 网站开发常用jQuery插件总结(11)折叠插件Akordeon
实现折叠菜单,可以完全不使用插件.如果使用jquery的话,实现起来也比较简单,我们只需要定义折叠菜单的样式,然后使用jquery中的渐隐渐现就可以了.如果我们自己写折叠菜单,可以方便的使用我们自己的 ...
- thinkphp ajax检测是否数据可用
模板文件:如 index.html 模板文件如:(index.html) <form><input type="text" name="username ...
- 固定滚动外层div的css
background-color: #2a3138; position: fixed; bottom: 0; left: 0; width: 100%; height: 57px; overflow: ...
- python 循环while和for in
#!/uer/bin/env python # _*_ coding: utf-8 _*_ lucknumber = 5 b = 0 while b <3: print('guss count: ...
- BZOJ 1052: [HAOI2007]覆盖问题
BZOJ 1052: [HAOI2007]覆盖问题 题意:给定平面上横纵坐标在-1e9~1e9内的20000个整数点的坐标,用三个大小相同边平行于坐标轴的正方形覆盖(在边界上的也算),问正方形的边长最 ...
- jquery直接获取html页面元素
大家都会用$('div')来获取div并对其进行一些操作,今天用到一个函数发现$('div')与getElementBy系列函数得到的对象并不一样. 然后去查了下,发现$('div')得到的是一个数组 ...