一.题目链接:https://leetcode.com/problems/4sum/

二.题目大意:

 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的。

三.题解:

这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或者双指针,此处使用的是双指针法。具体代码如下:

class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
int len = nums.size();
vector<vector<int>> rs;
if(len < 4)
return rs;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i - 1])
continue;
for(int j = i + 1; j < len - 1; j++)
{
if(j != i + 1 && nums[j] == nums[j -1])//注意此处的去重,要保证j不是第一次被使用,所以必须是j!=i+1
continue;
int l = j + 1, r = len - 1;
while(l < r)
{
if(nums[i] + nums[j] + nums[l] + nums[r] == target)
{
rs.push_back({nums[i],nums[j],nums[l],nums[r]});
l++;
r--;
while( l < r && nums[l] == nums[l - 1])
l++;
while(l < r && nums[r] == nums[r + 1])
r--;
}
else if(nums[i] + nums[j] + nums[l] + nums[r] < target)
l++;
else
r--; }
}
}
return rs; }
};

本例中的方法的时间复杂度为O(N^3),空间复杂度为O(1)。此外,之前在3sum问题中总结过:4sum问题还有时间复杂度为O(N^2)的算法,具体可见:https://www.cnblogs.com/wangkundentisy/p/9079622.html

这里有几点需要注意:

1.由于题目要求四元组是唯一的,所以要进行去重:每一个外层循环都需要去重,即i和j都要去重(对于ksum问题,也是这样的,k-1个外层循环都要按照这个规律进行去重)

2.外层循环的去重,要保证此处的指针不是第一次被使用,也就是说在该位置之前,应该确保已经执行过一次了(如果是第一次使用,就不用去重了),所以才用 i != 0和 j != i + 1这种判断条件,对于ksum问题,一定注意这种规律。

LeetCode——18. 4Sum的更多相关文章

  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. LeetCode 18 4Sum (4个数字之和等于target)

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

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

  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 15 3sum & leetcode 18 4sum

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

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

  8. 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 ...

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

随机推荐

  1. 检测IP地址冲突的shell脚本-check_server_ip_conflict.sh

    check_server_ip_conflict.sh 使用arping获取对应IP地址的MAC地址,如果和预料的不一致则报警: #!/bin/bash epg_addr_01="00:50 ...

  2. SEO:网站改版

    网站改版分为2种:前端页面改版(不使用301 ),链接结构发生变化(必须使用301) 1.确定一定以及肯定使用301永久重定向,不要使用302跳转 2.非常十分以及极其要求使用百度站长平台的“网站改版 ...

  3. HPU1460: 杨八方的表面兄弟

    题目描述 如果你之前关注过HPUOJ的话,那么你一定听说过杨八方的名字.在去年,很多同学共同见证了杨八方同学的填报志愿.来到学校.军训--或许你曾陪同杨八方一起思考过许多问题,又或者你是刚听说这个名字 ...

  4. 软件工程 week 01

    一.安装与使用Git First项目地址: https://git.coding.net/kefei101/First.git 二.针对以下三个问题,作为大三新生,谈谈我的感想 问题1:你为什么选择计 ...

  5. mybatis下的分页,支持所有的数据库

    大家都知道,mybatis的自带分页方法只是逻辑分 页,如果数据量很大,内存一定会溢出,不知道为什么开源组织不在里面集成hibernate的物理分页处理方法!在不修改mybatis源代码的情况下, 应 ...

  6. Linux Distribution 分支

    https://distrowatch.com/ DistroWatch是一个包含了各种Linux发行版及其他自由/开放源代码的类Unix操作系统的新闻.人气排名.以及其他一般信息等的网站.它包含了数 ...

  7. Java(命令行)打印库存清单

    public class demo{ public static void main(String[] args){ //1 顶部 System.out.println("--------- ...

  8. day 53 js学习之

    ---恢复内容开始--- 1.昨日作业讲解 弄一个上图一样的选择器,可以全选,可以反选,取消 <!DOCTYPE html> <html lang="zh-CN" ...

  9. Running Elixir in Docker Containers

    转自:https://www.poeticoding.com/running-elixir-in-docker-containers/ One of the wonderful things abou ...

  10. 最大值最小值(max,max_element)

    min 如果比不出大小就返回第一个引数 //版本一:调用operator< template <class LessThanComparable> const LessThanCom ...