[LeetCode1]3Sum

2023-11-01 15:38
文章标签 3sum leetcode1

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

Given an array S of n integers, are there elements abc 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) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

    For example, given array S = {-1 0 1 2 -1 -4},A solution set is:(-1, 0, 1)(-1, -1, 2)
Analysis

Two-pointer scan

O(n^2)

c++

vector<vector<int> > threeSum(vector<int> &num) {vector<vector<int>> result;sort(num.begin(),num.end());for(int i=0; i<num.size(); i++){int target = 0-num[i];int start = i+1;int end = num.size()-1;while(start<end){if(num[start]+ num[end] == target){vector<int> solution;solution.push_back(num[i]);solution.push_back(num[start]);solution.push_back(num[end]);result.push_back(solution);start++, end--;while(start<end && num[start] == num[start-1]) start++;while(start<end && num[end] == num[end+1]) end--;}else if(num[start] + num[end] >target)end--;elsestart++;}while(i<num.size() && num[i] == num[i+1]) i++;}return result;}
java

public List<List<Integer>> threeSum(int[] num) {List<List<Integer>> result = new ArrayList<List<Integer>>();Arrays.sort(num);int len = num.length;for(int i=0;i<len;i++){int target = 0-num[i];int start = i+1, end = len-1;while(start<end){if(num[start]+num[end]==target){ArrayList<Integer> content = new ArrayList<>();content.add(num[i]);content.add(num[start]);content.add(num[end]);result.add(content);start++;end--;while(start<end &&num[start]==num[start-1]) start++;while(start<end && num[end] == num[end+1]) end--;}else if(num[start]+num[end]>target){end--;}else {start++;}}while(i<len-1 && num[i]==num[i+1]) i++;}return result;}


在java中可以用Arrays的静态排序方法,。

用暴力搜索,三个循环,O(n^3)不能通过大数据

public List<List<Integer>> threeSum(int[] num) {List<List<Integer>> result = new ArrayList<List<Integer>>();for(int i=0;i<num.length;i++){int temp = 0-num[i];for(int j=i+1;j<num.length;j++){for(int k=j+1;k<num.length;k++){if(num[j]+num[k]==temp){ArrayList<Integer> content = new ArrayList<>();content.add(i);content.add(j);content.add(k);result.add(content);}}}}return result;}



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



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

相关文章

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