当前位置: 首页 > news >正文

15. 三数之和(Java)

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例 1:

输入: nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入: nums = [0,1,1]
输出:[]
解释: 唯一可能的三元组和不为 0 。

示例 3:

输入: nums = [0,0,0]
输出:[[0,0,0]]
解释: 唯一可能的三元组和为 0 。

提示:

3 <= nums.length <= 3000
-105 <= nums[i] <= 105

一开始的想法(三重循环),但是超时了:

class Solution {public List<List<Integer>> threeSum(int[] nums) {Arrays.sort(nums);List<List<Integer>> results = new ArrayList<List<Integer>>();List<Integer> result = new ArrayList<Integer>();HashMap<List<Integer>, List<Integer>> map = new HashMap<>();
//        int n = 1;// 三重循环,超时for (int i = 0; i < nums.length; i++){for (int j = i+1; j < nums.length; j++){for (int k = j+1; k < nums.length; k++){if (nums[i] + nums[j] + nums[k] == 0){result.add(nums[i]);result.add(nums[j]);result.add(nums[k]);// System.out.println(result);// 判断是否重复if (!map.containsValue(result)){map.put(result, result);
//                            n++;results.add(result);}// System.out.println(results);result = new ArrayList<Integer>();// System.out.println(result);}}}}return results; }
}

最终代码:

class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> ans = new ArrayList<>();if (nums == null || nums.length <= 2) return ans;Arrays.sort(nums); //从小到大排序,O(nlogn)for (int i=0; i < nums.length - 2; i++){if (nums[i] > 0) break; //第一个数大于0,后面的数都比它大,肯定不成立if (i > 0 && nums[i] == nums [i-1]) continue; //去掉重复的情况int target = -nums[i];int left = i + 1;int right = nums.length - 1;while (left < right){if (nums[left] + nums[right] == target){ans.add(new ArrayList<>(Arrays.asList(nums[i], nums[left], nums[right])));// 现在要增加 left,减小 right,但是不能重复,比如: [-2, -1, -1, -1, 3, 3, 3], i = 0, left = 1, right = 6, [-2, -1, 3] 的答案加入后,需要排除重复的 -1 和 3//增加left和减少right                  left++; right--;//去重while(left < right && nums[left] == nums[left - 1]) left++;while(left < right && nums[right] == nums[right +1]) right--;}else if(nums[left] + nums[right] < target){//两个数相加小于target,左指针向右移left++;}else if(nums[left] + nums[right] > target){//两个数相加大于target,右指针向左移right--;}}}return ans;}
}
http://www.lryc.cn/news/45559.html

相关文章:

  • Navicat Premium 16安装教程
  • 蓝桥杯刷题冲刺 | 倒计时8天
  • 四.JAVA基础面试题:重要知识
  • 某面试官分享经验:看求职者第一眼,开口说第一句话,面试结果就差不多定了,准确率高达90%以上...
  • Java开发 - 消息队列之RabbitMQ初体验
  • 蓝桥杯入职项目(HTML + springBoot)
  • 【IAR工程】STM8S208RB基于ST标准库下按键检测
  • 【5】深度学习之Pytorch——如何使用张量处理文本数据集(语料库数据集)
  • 《Spring系列》第5章 refresh()
  • ThreeJS-缩放、旋转(四)
  • 数据更新 | CnOpenData法拍房数据
  • 【Spring从成神到升仙系列 五】从根上剖析 Spring 循环依赖
  • 设计模式之代理模式(C++)
  • c++11 标准模板(STL)(std::unordered_multimap)(三)
  • Linux进程控制-2
  • 快速排序算法
  • 中华好诗词大学季第二季(四)
  • 分布式系统容灾部署方案
  • Python 爬虫性能相关总结
  • Baumer工业相机堡盟工业相机如何设置网口的IP地址(工业相机连接的网口设置IP地址步骤)
  • Android MediaCodec设置H264 Profile到High
  • QT之QSysInfo(查看电脑信息)
  • 中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年
  • 从头用脚分析FFmpeg源码 - av_read_frame
  • 第17章_触发器
  • 3956. 截断数组
  • React Labs: 我们最近在做什么——2023 年 3 月
  • 文件系统设计详解
  • 好看~立马启动python实现美女通通下
  • Git 安装设置