leetcode15-3Sum

2024-04-30 08:28
文章标签 leetcode15 3sum

本文主要是介绍leetcode15-3Sum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

给你一个整数数组 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] 。
注意,输出的顺序和三元组的顺序并不重要。

分析

可以想一想2Sum的解法,所以我们整体的思路就是固定一个元素,然后求剩下的元素当中哪俩个元素的和满足(0-固定的这个元素)的大小。同时我们可以有一些优化的地方,我们在排序以后固定元素的时候如果发现元素比0大,这种可以直接break了,因为比0大的元素不可能再和其它元素相加等于0,然后如果上一个元素和当前元素相同的这种重复场景也可以忽略
最终由于list需要唯一,可以借助set来实现

import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;public class threeSum {public static void main(String[] args) {int[] arr = {-1,0,1,2,-1,-4};List<List<Integer>> lis = getLis(arr);for(List<Integer> li : lis) {li.stream().forEach(System.out::println);System.out.println();}}public static List<List<Integer>> getLis(int[] nums) {int len = nums.length;Arrays.sort(nums);Set<List<Integer>> set = new HashSet();for(int i = 0;i<len-2;i++) {if(nums[i] > 0) {break;}if(i > 0 && nums[i-1] == nums[i]) {continue;}int j = i+1;int k = len - 1;int target = 0 - nums[i];while(j<k) {if(nums[j] + nums[k] == target) {List<Integer> lis = new ArrayList();lis.add(nums[i]);lis.add(nums[j]);lis.add(nums[k]);j++;k--;set.add(lis);} else if(nums[j]+nums[k] < target) {j++;} else {k--;}}}List<List<Integer>> res = new ArrayList();res.addAll(set);return res;}
}

这篇关于leetcode15-3Sum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/948366

相关文章

LeetCode - 15. 3Sum

15. 3Sum Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数列,找出这个数列中和为0的三元组. analyse: 时间复杂度:O(n^2) 思路很简单,注意一下去重的方法. Time comple

LeetCode 16 3Sum Closest

题意: 给出数组s和目标target,要求从中选出3个数字,使其相加的和最接近target。 思路: x sum系列的题目,在这里做一个总结。 最经典的情况为2 sum问题,即给出s和target找出s[i] + s[j] = target。 可以使用枚举s[i]判断target - s[i]是否在s中出现且与s[i]不同的O(nlogn)方法,用map或排序后二分查找的方式均可。

3Sum java leetcode

Given an array S of n integers, are there elementsa,b, c in S such that a + b +c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain dupl

3Sum Closest问题及解法

问题描述: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have

【LeetCode最详尽解答】15-三数之和 3sum

欢迎收藏Star我的Machine Learning Blog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star, 有问题可以随时与我交流, 谢谢大家! 链接: 15-三数之和 直觉 示例: 输入: nums = [-1, 0, 1, 2, -1, -4] 输出: [[-1, -1, 2], [-1

Leet Code Medium 15 3Sum

【题目】 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. Note: Elements in a triplet

[LeetCode] 015--3Sum --Medium--

3Sum 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. Note: Elements in a triplet (a,b,c)

LeetCode 题解(270) : 3Sum Smaller

题目: Given an array of n integers nums and a target, find the number of index tripletsi, j, k with 0 <= i < j < k < n that satisfy the conditionnums[i] + nums[j] + nums[k] < target. For example, gi

[LeetCode] 16. 3Sum Closest

题目内容 https://leetcode-cn.com/problems/3sum-closest/ 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。 题目思路 我用的暴力破解法。先把数组排序好,然后开始定位。先定位好两个数,然后来看第三个数。因为

《leetCode》:3Sum Closest

题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exa