n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数

重复数处理:

使i为左至右第一个不重复数:while(i != 0 && i<n-2 && a[i] == a[i-1]) i++;

使r为右至左第一个不重复数(原序最后一个):while(r>i && r+1<n && a[r] == a[r+1]) r--;

15. 3Sum

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
int n = nums.size();
vector<int> &a = nums;
for(int i=; i<n-; i++) {
while(i != && i<n- && a[i] == a[i-]) i++;
int l = i+, r = n-;
while(l < r) {
int s = a[i]+a[r]+a[l];
if(s == ) {
ans.push_back({a[i], a[l], a[r]});
l++; r = n-;
while(l<r && a[l] == a[l-]) l++;
} else if(s < ) {
l++;
while(l<r && a[l] == a[l-]) l++;
} else {
r--;
while(r>i && r+<n && a[r] == a[r+]) r--;
}
}
}
return ans;
}
};

16. 3Sum Closest

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
int mind = INT_MAX, ans = ;
vector<int> &a = nums;
for(int i=; i<n-; i++) {
int l = i+, r = n-;
while(l < r) {
int s = a[i]+a[r]+a[l];
int d = abs(s - target);
if(s == target)
return s;
else if(d < mind) {
mind = d;
ans = s;
}
if(s < target)
l++;
else
r--;
}
}
return ans;
}
};

18. 4Sum

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<int> &a = nums;
sort(a.begin(), a.end());
vector<vector<int>> ans;
int n = a.size();
if(n < ) return ans;
for(int i=; i<n-; i++) {
while(i != && i<n- && a[i] == a[i-]) i++;
for(int j=i+; j<n-; j++) {
while(j != i+ && j<n- && a[j] == a[j-]) j++;
int l = j+, r = n-;
while(l < r) {
int s = a[i]+a[j]+a[r]+a[l];
if(s == target) {
ans.push_back({a[i], a[j], a[l], a[r]});
l++; r = n-;
while(l<r && a[l] == a[l-]) l++;
} else if(s < target) {
l++;
while(l<r && a[l] == a[l-]) l++;
} else {
r--;
while(r>j && r+<n && a[r] == a[r+]) r--;
}
}
}
}
return ans;
}
};

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

  1. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

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

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

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

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

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

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

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

  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. 《LeetBook》leetcode题解(16):3Sum Closest [M]

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

随机推荐

  1. HDU 1542 Atlantis(扫描线)题解

    题意:给n个可能相交的矩形,问你不重复的总面积 思路:扫描线,一边扫一边加. 扫描线:图片来源:理解扫描线 假设我们要算以下四个矩形面积,显然中间深色的是重复的.我们按照x的大小,从左往右扫,然后用线 ...

  2. SpringMVC成员变量并发状态下使用测试

    1.SpringMVC默认是单例的,使用成员变量在并发状态下该成员变量的值是被共享的 测试平台 我们目前正在开发的电商项目  (架构组成SpringCloud + SpringBoot + Sprin ...

  3. Predict Referendum by sklearn package

    Background Last day we talk about Python Basics in Chinese. Today, we will do data analysis with pyt ...

  4. (js) 字符串和数组的常用方法

    JS中字符串和数组的常用方法 JS中字符串和数组的常用方法 js中字符串常用方法 查找字符串 根据索引值查找字符串的值 根据字符值查找索引值 截取字符串的方法 字符串替换 字符串的遍历查找 字符串转化 ...

  5. 16_Linux网络配置

    A类:255.0.0.0        8 0 000 0001 - 0 111 1111 127用户回环,1-126 2^7-1个A类地址 容纳多少个主机:2^24-2 主机位全0:网络地址 主机位 ...

  6. swift 有道 翻译文档(1 定义变量常量,数组字典)

    使用let来创建常量,使用var来创建变量.一个常量的值在编译时不需要知道,但是您必须为它指定一个值一次.这意味着您可以使用常量来命名一个您确定一次的值,但是在许多地方使用它.var myVariab ...

  7. 从Scratch到Python——python turtle 一种比pygame更加简洁的实现

    从Scratch到Python--python turtle 一种比pygame更加简洁的实现 现在很多学校都开设了Scratch课程,学生可以利用Scratch创作丰富的作品,然而Scratch之后 ...

  8. UVA10163 Storage Keepers (动态规划)

    $dp[i][j]$表示前$i$个仓库由前$j$个人来守卫能取得的最大安全值: $cost[i][j]$表示前$i$个仓库由前$j$个人来守护在能取得的最大安全值的情况下的最小花费. AC代码 //# ...

  9. java mvn:安装jar包

    mvn install:install-file -Dfile=fastdfs-client-java-1.27-SNAPSHOT.jar(路径) -DgroupId=org.csource -Dar ...

  10. 如何利用redis来进行分布式集群系统的限流设计

    在很多高并发请求的情况下,我们经常需要对系统进行限流,而且需要对应用集群进行全局的限流,那么我们如何类实现呢. 我们可以利用redis的缓存来进行实现,并且结合mysql数据库一起,先来看一个流程图. ...