当前位置:网站首页>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;
}
}
边栏推荐
- 【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
- 04-SDRAM:读操作(突发)
- 双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
- 【Go】Go 语言切片(Slice)
- Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
- Kubernetes调度
- 4-1-7 二叉树及其遍历 家谱处理 (30 分)
- 数据库概论 - MySQL的简单介绍
- DDL+DML+DQL
- 【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
猜你喜欢

Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?

机器学习反向传播的一些推导公式

Log4net 思维导图

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie

Basic usage of Koa framework

【微服务】(十六)—— 分布式事务Seata

事务的传播机制

批量免费文字翻译
随机推荐
Web浏览器工作流程解析
机器学习反向传播的一些推导公式
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
关于求反三角函数的三角函数值
电压源的电路分析知识分享
从入门到一位合格的爬虫师,这几点很重要
数据库原理作业3 — JMU
03-SDRAM:写操作(突发)
【面试:并发篇37:多线程:线程池】自定义线程池
Conditional statements of shell (test, if, case)
Project exercise - memorandum (add, delete, modify, check)
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
一文读懂 MongoDB 和 MySQL 的差异
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
事务的传播机制
熟悉而陌生的新朋友——IAsyncDisposable
科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
浅层了解欧拉函数