题意

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

找出一个数组中的三个数,使这三个数的和为0。输出所有的组合,不能重复。

解法

最简单的思路就是跑一个三层循环,暴力枚举所有组合,很显然会超时。

然后考虑排序后跑两层循环,第三层改用二分查找,即确定前两个数后用二分来搜第三个数,时间复杂度降到了O(logN * N^2),还是会超时。

最后,采用了Two Sum这一题的办法,遍历第一个数,然后剩下的两个数用双指针算法来找,这样时间复杂度就降到了O(N^2)

还有一个问题是判重,这里采用的办法是将三个数拼接起来成为一个数,比如【-1,0,1】就被保存成-101,用Long Long来存,然后放到一个Map里,每次选取新答案时都判断一下这样的组合是不是能在Map里找到。

class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
map<long long,bool> vis;
sort(nums.begin(),nums.end()); vector<vector<int>> rt;
for(int i = 0;i < nums.size();i ++)
{
if(nums[i] > 0)
break;
int j = i + 1;
int k = nums.size() - 1;
while(j < k)
{
if(nums[i] + nums[j] + nums[k] == 0)
{
long long box = abs(nums[i]); // 判重
int temp = abs(nums[j]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[j]);
temp = abs(nums[k]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[k]);
if(nums[i] * nums[j] * nums[k] < 0)
box = -box; if(vis.find(box) == vis.end())
{
vis[box] = true;
rt.push_back({nums[i],nums[j],nums[k]});
}
j ++;
} if(nums[i] + nums[j] + nums[k] < 0)
j ++;
else if(nums[i] + nums[j] + nums[k] > 0)
k --;
}
}
return rt;
}
};

LeetCode 3Sum (Two pointers)的更多相关文章

  1. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  2. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

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

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  5. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  6. leetcode — 3sum

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...

  7. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  8. LeetCode: 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

随机推荐

  1. ulimit linux文件配置

    文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符.在程序设计中,一 ...

  2. 【转】Java学习---算法那些事

    [更多参考] LeetCode算法 每日一题 1: Two Sum ----> 更多参考[今日头条--松鼠游学] 史上最全的五大算法总结 Java学习---7大经典的排序算法总结实现 程序员都应 ...

  3. ShellCode初体验

    写在前面的话: ShellCode是一门艺术,就像围棋手门追求的“神之一手”,今天就来初探一下这让人疯狂的艺术: 零.代码0 相信手写opcode,目前很少有人干了,其实,也确实已经没有这个必要了,毕 ...

  4. 1、爬虫简介与request模块

    一 爬虫简介 概述 近年来,随着网络应用的逐渐扩展和深入,如何高效的获取网上数据成为了无数公司和个人的追求,在大数据时代,谁掌握了更多的数据,谁就可以获得更高的利益,而网络爬虫是其中最为常用的一种从网 ...

  5. C++构造析构函数生命期及对象生命期

  6. 【Hibernate步步为营】--核心对象+持久对象全析(一)

    引言         上篇博客对Hibernate进行了基本的解析.并分析了它的一些特性. Hibernate可以如此的流行也是由于它有诸多长处,不论什么事物都有两面性.Hibernate尽管流行.可 ...

  7. Mac svn使用学习-2-服务端

    2.在mac环境下搭建一个SVN服务器环境 1)创建一个名为myCode的仓库——svnadmin命令 格式: svnadmin SUBCOMMAND REPOS_PATH [ARGS & O ...

  8. unset MAILCHECK

    文件/etc/profile尾部有: unset MAILCHECK 为了解决:每次登陆linux总是提示:you hava a new mail

  9. openresty火焰图安装

    1.下载systemtap安装包并安装 从https://sourceware.org/systemtap/ftp/releases/下载最新版的systemtap.tar.gz压缩包 我安装的时候, ...

  10. 微信小程序 bindcontroltap 绑定 没生效

    微信 bindcontroltap 绑定 没生效 多次查看官方文档,代码没有问题, 解决: windows下  显示设置中 缩放必须是100%,