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. xftp5和xshell的使用

    目的:实现windows和linux系统之间文件的传输 步骤:1)下载xshell5 百度搜索即可,直接下载安装 2)xshell5下载完成后与虚拟机链接成功 3)下载xftp5点击 然后跟据官方的指 ...

  2. 【ASP.NET】 HttpContext.Current.User.Identity.Name 返回值为空

    问题起因 在做项目的时候,我使用HttpContext.Current.User.Identity.Name来获取Web应用程序正在使用时的用户名. 在开发过程中,我使用了我的本地iis,启用了集成的 ...

  3. java三种注释以及参数涵义(转)

    原文地址:https://www.cnblogs.com/miys/p/4bf714ce33068dcf9ac6526309c9b5e6.html 单行注释:// 注释内容 多行注释:/*... 注释 ...

  4. java知识点集锦--基础知识部分

    1.面向对象特征:封装.继承.多态 封装:把数据和操作数据的方法绑定起来,对数据的访问只能通过已定义的接口来访问.面向对象的本质就是将现实世界描绘成一系列完全自治.封闭的对象.我们在类中编写的方法就是 ...

  5. 换工作之后需要兼容ie8的我

    以下是我ie8踩得坑,总结了一下,以免以后会遇到,虽然有的度娘也能搜到但是偶尔看看自己的文章也能学到很多(后续如有添加继续补上) 1,ie8 input框建议不要使用line-height去撑高度,在 ...

  6. fiddler -- 一个强大的抓包工具

    一.fiddler常用功能: 1. Fiddler 是位于客户端和服务器端的http代理,也是目前最常用的http抓包工具之一.它能够记录客户端和服务器之间的所有http请求,可以针对特定的http请 ...

  7. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  8. 浅谈Pool对象

    Pool对象的技术指标: 避免频繁创建经常使用的稀有资源,提高工作效率. 控制阀值,很多情况下一些关键资源都有一个最佳并发数,超过这个拐点性能有可能急剧下降,也有可能继续增大并发数性能不能提升. 安全 ...

  9. SWUST OJ(1035)

    定位顺序表中最大和最小值 #include<iostream> #include<cstdlib> using namespace std; int main(int argc ...

  10. MySQL Packets larger than max_allowed_packet are not allowed

    MySQL的一个系统参数:max_allowed_packet,其默认值为1048576(1M), 查询:show VARIABLES like '%max_allowed_packet%'; 修改此 ...