15 3sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

思考:1、想了很久,不知道怎么做。只能想到时间复杂度比较高的做法。在网上查到,这个3sum问题是比较经典的,其解法基于2sum。2sum问题是给定一个排好序的数组,问有哪些两个元素相加为
0。解法是一个下标指向第一个,另一个下标指向最后一个。
有如下结论及其规则:
一、如果num[1]+num[n]>0,那么2和n的和大于0,3和n的和也会大于0。等等。所以就没必要再去计算以最后一个为右边界的组合。考虑1,..,n-1这个范围内的组合,即移动后一个。
二、如果num[1]+num[n]<0,那么1和n-1的和,1和n-2的和也会小于0。等等。所以就没必要去计算以1为左边界的组合。考虑2,...,n这个范围内的组合,即移动前一个下标。
不断进行这个操作,最终遍历所有可能的组合,而不是每个组合。 3sum问题可以分解成2sum。取出数组第一个元素X,在剩下的数组里找到两数之和为-X,这就是2sum问题。 由于c语言不支持动态添加元素,所以用c++可能更方便一些,故实现如下。ps:用c语言做了一下,一直free内存那里出问题。
 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int> > ans;
int index1,index2;
if(nums.size()<) return ans; sort(nums.begin(), nums.end()); for (int i = ; i <= nums.size()-; i++) {
if(i!= && nums[i-]==nums[i]) continue; index1 = i+;
index2 = nums.size()-;
/*if(i==0) {
index1 = 1;
index2 = nums.size()-1;
}
else if (i== nums.size()-1) {
index1 = 0;
index2 = nums.size()-2;
}else {
index1 = 0;
index2 = nums.size()-1;
}*/ while(index1<index2) {
if(nums[i]+nums[index1]+nums[index2]==) {
int sol[] = { nums[i], nums[index1], nums[index2] };
ans.push_back(vector<int> (sol, sol + ));
//printf("i is %d.index1 is %d.index2 is %d.\n", i,index1,index2);
index1++;while(nums[index1]==nums[index1-]) index1++;
index2--;while(nums[index2]==nums[index2+]) index2--;
}else if (nums[i]+nums[index1]+nums[index2]>) {
index2--;
}else {
index1++;
}
}
}
return ans;
}
};

16 3sum closet

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

和上面是简单变形关系,所用的原理一样。因为此次并不需要返回数组的数组,所以又用c语言来表示我的行为。

 void my_qsort(int *nums, int index1, int index2) {
if(index1==index2) return; int pre,last;
int tmp; pre = index1;
for(last=index1+; last<=index2; last++) {
if(nums[last]<nums[index1]) {
pre++;
tmp = nums[pre];
nums[pre] = nums[last];
nums[last] = tmp;
}
}
tmp = nums[index1];
nums[index1] = nums[pre];
nums[pre] = tmp; if(pre==index1) {
my_qsort(nums, index1+, index2);
}else if(pre==index2) {
my_qsort(nums, index1, index2-);
}else {
my_qsort(nums, index1, pre-);
my_qsort(nums, pre+, index2);
} } int threeSumClosest(int* nums, int numsSize, int target) {
int ret,index1,index2,tmp;
my_qsort(nums, , numsSize-);
for(int i = ; i<numsSize; i++)
printf("%d ", nums[i]);
printf("\n");
ret = nums[] + nums[] + nums[];
for(int i=; i<=numsSize-;i++) {
index1 = i+;
index2 = numsSize-;
while(index1<index2) {
tmp = nums[i] + nums[index1] + nums[index2];
if(tmp == target) return tmp;
if(abs(tmp-target)<abs(ret-target)) ret = tmp;
if(tmp>target) {
index2--;
}else if(tmp < target) {
index1++;
}
}
} return ret;
}

18. 4Sum

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

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [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]
]

思考:首先按一定顺序取前两个数,共有(n-3)*(n-2)个组合,对于剩下的两个数就用首尾指针进行移动检查。
这里我有一个小小的优化。看下line 12行的代码,如果nums[i]大于0,nums[i]>terget,那么后面的三个数一定大于0。既然后面的三个数都大于0,nums[i]加上大于的0
  一定大于target。所以就没有必要再继续检查。第15行代码同理。
  
 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> ans; sort(nums.begin(), nums.end()); int len = nums.size();
int i,j,k,l; for(i=; i<=len-; i++) {
if(nums[i]>target && nums[i]>=) continue;
if(i>= && nums[i]==nums[i-]) continue;
for(j=i+; j<=len-; j++) {
if(nums[i]+nums[j]>target && nums[j]>=) continue;
if(j>=i+ && nums[j]==nums[j-]) continue;
k = j + ;
l = len - ;
while(k<l) {
if(nums[i]+nums[j]+nums[k]+nums[l]>target) {
l--;
}else if(nums[i]+nums[j]+nums[k]+nums[l]<target){
k++;
}else {
int sol[] = { nums[i], nums[j], nums[k],nums[l] };
ans.push_back(vector<int> (sol, sol + ));
k++;
while(nums[k]==nums[k-]) k++;
l--;
while(nums[l]==nums[l+]) l--;
} } }
}
return ans;
}
};

15. 3Sum、16. 3Sum Closest和18. 4Sum的更多相关文章

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

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

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

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

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

  5. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  6. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  8. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  9. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

随机推荐

  1. 杭电-------2043密码(C语言写)

    #include<stdio.h> #include<string.h> ]; ] = { '~','!','@','#','$','%','^' }; ] = { }; in ...

  2. Vue生命周期和钩子函数及使用keeplive缓存页面不重新加载

    Vue生命周期 每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期,在这个过程中会有一些钩子函数会得到回调 Vue中能够被网页直接使用的最小单位就是组件,我们经常写的: ...

  3. debian 10安装英伟达独显驱动

    我的显卡是GTX1050TI,刚安装好Debian 10的时候启动会黑屏,无法进入系统,解决办法是在grub界面,按e修改启动参数,在启动参数那一行(一般会包含quiet)后面加上 nouveau.m ...

  4. 彻底搞懂flex弹性盒模型布局

    为什么要用flex 基于css3简单方便,更优雅的实现,浏览器兼容性好,传统的css实现一个div居中布局要写一堆代码,而现在几行代码就搞定了,没有理由不用flex. 兼容性: Base Browse ...

  5. proptypes介绍

    开始 prop-types的主要作用:对props中数据类型进行检测及限制 引用方法:import PropTypes from 'prop-types' 用法: // 基本用法 用来检测数据类型 c ...

  6. 为NuGet配置微软官方中国镜像

    NuGet微软官方中国镜像地址: https://nuget.cdn.azure.cn/v3/index.json 打开Visual Studio => 工具 => NuGet包管理器 = ...

  7. C# aggregateexception flatten innerexceptions

    static void AggregateExceptionsDemo() { var task1 = Task.Factory.StartNew(() => { var child1 = Ta ...

  8. Html介绍,了解html代码的注释

    什么是代码注释?在代码编辑器中的注释代码是不会在浏览器窗口展示的!html代码注释的作用是帮助程序员标注代码的用途,过一段时间后再看你之前编写的代码,很快可以想起这个代码的用途.代码注释不仅方便程序员 ...

  9. 清北学堂—2020.1提高储备营—Day 4 morning(数论)

    qbxt Day 4 morning --2020.1.20 济南 主讲:李奥 目录一览 1.一些符号与基本知识 2.拓展欧几里得,逆元与欧拉定理 3.线性筛法与积性函数(非重点) 总知识点:数论 一 ...

  10. 部署Nexus作为docker的私有仓库

    目录 Docker搭建Nexus私有仓库... 1 一.安装部署... 1 1.安装... 2 2.访问网页端... 2 二.配置使用... 2 1.创建本地仓库... 2 2.docker配置... ...