当前位置:网站首页>2022.07.22_每日一题
2022.07.22_每日一题
2022-07-31 06:07:00 【诺.い】
15. 三数之和
题目描述
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例 2:
输入:nums = []
输出:[]
示例 3:
输入:nums = [0]
输出:[]
提示:
0 <= nums.length <= 3000-105 <= nums[i] <= 105
- 数组
- 双指针
- 排序
coding
// 愿天堂不需要去重、、、
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length < 3) {
return res;
}
Arrays.sort(nums);
for (int first = 0; first < nums.length; first ++) {
if (nums[first] > 0) {
break;
}
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
int second = first + 1;
int third = nums.length - 1;
while (second < third) {
if (nums[second] + nums[first] + nums[third] == 0) {
res.add(Arrays.asList(new Integer[]{
nums[first], nums[second], nums[third]}));
while (second < third && nums[second] == nums[second + 1]) {
second ++;
}
while (second < third && nums[third] == nums[third - 1]) {
third --;
}
second ++;
third --;
}else if (nums[second] + nums[first] + nums[third] < 0) {
second ++;
} else {
third --;
}
}
}
return res;
}
}
边栏推荐
- 【云原生】3.3 Kubernetes 中间件部署实战
- MySQL系列一:账号管理与引擎
- 数据库原理作业2 — JMU
- Bulk free text translation
- 【云原生】-Docker容器迁移Oracle到MySQL
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
- 文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
- 高并发与多线程之间的难点对比(容易混淆)
- Kubernetes scheduling
- 从入门到一位合格的爬虫师,这几点很重要
猜你喜欢

【编程题】【Scratch三级】2022.03 冬天下雪了

批量翻译软件免费【2022最新版】

电压源的电路分析知识分享

把 VS Code 当游戏机

One of the small practical projects - food alliance ordering system

【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建

Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)

【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念

【解决】npm ERR A complete log of this run can be found in npm ERR

英语翻译软件-批量自动免费翻译软件支持三方接口翻译
随机推荐
Kubernetes scheduling
Moment.js common methods
外贸网站优化-外贸网站优化教程-外贸网站优化软件
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
文件 - 05 下载文件:根据文件Id下载文件
DirectExchange switch simple introduction demo
【科普向】5G核心网架构和关键技术
试题 历届真题 错误票据【第四届】【省赛】【B组】
LeetCode刷题——摆动序列#376#Medium
MySQL的触发器
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
Log4net 思维导图
文件 - 04 下载文件: 根据文件下载链接下载文件
第三方库-store
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
毫米波技术基础
电压源的电路分析知识分享
深度学习通信领域相关经典论文、数据集整理分享
Database Principles Homework 2 — JMU