68 三数之和(3Sum)
文章目录
- 1 题目
- 2 解决方案
- 2.1 思路
- 2.2 时间复杂度
- 2.3 空间复杂度
- 3 源码
1 题目
题目:三数之和(3Sum)
描述:给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。三元组(a, b, c)要求a≤b≤c。结果不能包含重复的三元组。
lintcode题号——57,难度——medium
样例1:
输入:numbers = [2,7,11,15]
输出:[]
解释:找不到三元组使得三个数和为0。
样例2:
输入:numbers = [-1,0,1,2,-1,-4]
输出:[[-1, 0, 1],[-1, -1, 2]]
解释:[-1, 0, 1]和[-1, -1, 2]是符合条件的2个三元组。
2 解决方案
2.1 思路
考虑循环遍历数组,固定其中一个数,再在剩下的子数组中找到和为目标值的两数即可。
2.2 时间复杂度
外层循环时间复杂度O(n),twoSum的时间复杂度O(n),总时间复杂度为O(n^2)。
2.3 空间复杂度
使用了vector数据结构,空间复杂度为O(n)。
3 源码
细节:
- 先对数组进行排序。
- 使用循环固定其中一个数,再进行twoSum——在子数组中找到和为目标值的两数。
- 因为需要去重,所以先排序,跳过与前一个数相同的数,在第一个数的循环和twoSum循环中都需要做去重。
C++版本:
/**
* @param numbers: Give an array numbers of n integer
* @return: Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int>> threeSum(vector<int> &numbers) {// write your code herevector<vector<int>> results;if (numbers.empty()){return results;}// 先对数组排序sort(numbers.begin(), numbers.end());// 固定其中一个数,再对后面的数组进行twoSum()for (int i = 0; i < numbers.size() - 2; i++){//第一个数去重if (i != 0 && numbers.at(i) == numbers.at(i - 1)){continue;}int target = -numbers.at(i);vector<vector<int>> temp = twoSum(numbers, i + 1, numbers.size() - 1, target);for (auto it : temp){it.insert(it.begin(), numbers.at(i));results.push_back(it);}}return results;
}vector<vector<int>> twoSum(vector<int> & numbers, int left, int right, int target)
{vector<vector<int>> results;while (left < right){if (numbers.at(left) + numbers.at(right) < target){left++;}else if (numbers.at(left) + numbers.at(right) > target){right--;}else{vector<int> temp;temp.push_back(numbers.at(left));temp.push_back(numbers.at(right));results.push_back(temp);left++;right--;// twoSum中也需要去重while (left < right && numbers.at(left) == numbers.at(left - 1)){left++;}while (left < right && numbers.at(right) == numbers.at(right + 1)){right--;}}}return results;
}
68 三数之和(3Sum)相关推荐
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- [Swift]LeetCode15. 三数之和 | 3Sum
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- [Swift]LeetCode16. 最接近的三数之和 | 3Sum Closest
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LeetCode 16. 最接近的三数之和 3Sum Closest
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- Leetcode 16. 最接近的三数之和(3Sum Closest)
解法一: class Solution { public:int threeSumClosest(vector<int>& nums, int target) {int close ...
- 【算法】3Sum Closest 最接近的三数之和
文章目录 3Sum Closest 最接近的三数之和 问题描述: 分析 代码 二分 双指针 Tag 3Sum Closest 最接近的三数之和 问题描述: 给你一个长度为 n 的整数数组 nums 和 ...
- leetcode 16. 3Sum Closest | 16. 最接近的三数之和(双指针)
题目 https://leetcode.com/problems/3sum-closest/ 题解 方法1:固定 L,双指针找 M.R 时间复杂度 O(n^2),推荐此方法. 证明不会有元素遗漏,详见 ...
- 伍六七带你学算法 进阶篇-三数之和
三数之和 难度-中等 题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意 ...
- leetcode No.15-16 三数之和相关问题
leetcode 15. 三数之和 题目 链接:https://leetcode-cn.com/problems/3sum 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 ...
最新文章
- WIN7 x64 32位的IE8 打开就崩溃,停止工作
- kbmmw 的HTTPSmartService入门
- boost::contract模块实现ifdef宏功能测试程序
- 微软遭遇滑铁卢,chrome成为最受欢迎浏览器
- 3、PV、UIP、UV指的是什么
- com.jhlabs:imaging:jar:01012005 所在仓库+captcha验证码maven依赖
- 每日一题(1) —— 数组计算
- python列表注解
- EXCEL 将选中列改为只读
- Python绘图实例33:太极图绘制
- No1.初来乍到,请多指教
- 【Rust日报】2022-07-20 极简主义 Poem 指南
- win7下l2pt/sec 的789报错解决
- 爬虫获取网易云音乐单曲或歌单实现音乐闹钟
- 数据中台的五个关键要素
- oracle中时间差转秒,oracle计算时间秒数差
- 用C语言“调戏”qq
- PhotoShop高级应用
- 第20篇 项目进度管理__项目进度管理的含义和作用
- 用animation实现钟表动画
热门文章
- int型与double型
- 车辆重识别:Flare-Aware Cross-modal Enhancement Network for Multi-spectral Vehicle Re-identification
- 【lammps案例教学+ReaxFF力场开发应用】
- 一边深耕场景化应用一边构建DUI生态,AI生态这盘棋思必驰要怎么下?
- PyAutoGui图像操作(二):图像定位不稳定解决方案
- 二本计算机专业录取分数查询,二本录取查询
- 图像处理:Python代码计算图像对比度
- 搭建sonarqube服务器并查看测试结果
- 笔记-【游戏制作教程】30分钟制作一款游戏 (1)【Unity】
- Centrex业务-呼叫转移