当前位置:网站首页>[force deduction] sum of three numbers
[force deduction] sum of three numbers
2022-07-23 20:28:00 【aigo-2021】
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 And No repetition Triple of .
Be careful : Answer cannot contain duplicate triples .
Example 1:
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Example 2:
Input :nums = []
Output :[]
Example 3:
Input :nums = [0]
Output :[]
Tips :
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
Mode one : adopt Set Gather to judge
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list=new ArrayList<>();
if(nums.length<3) return list;
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
// If the first element after sorting is greater than 0, Then the following elements are greater than 0, non-existent 3 The sum of the numbers is 0
if(nums[i]>0) break;
// First number
int first=nums[i];
// Exclude duplicates
if(i>0&&nums[i]==nums[i-1]) continue;
Set<Integer> set=new HashSet<>();// Put it in the outer circulation , Each cycle is recreated set aggregate
for(int j=i+1;j<nums.length;j++){
// The third number
int third=nums[j];
int second=-(first+third);// The first number and the second number add and negate to form the third number
if(set.contains(second)){
//Arrays.asList() Convert array to List aggregate
list.add(new ArrayList<>(Arrays.asList(first,third,-(first+third))));
while(j < nums.length - 1 && nums[j] == nums[j + 1]) j++;// duplicate removal
}
set.add(third);// Store the third number in the set , Because the third number is the original number in the array , Use it with second Compare , Instead of putting second Deposit in set in
}
}
return list;
}
}
Mode two : Double pointer
ublic class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list=new ArrayList<>();
// Sentenced to empty , And judge whether the array length is less than 3
if(nums==null || nums.length<3) return list;
Arrays.sort(nums); // Sort
for(int i=0;i<nums.length-1;i++){
if(nums[i]>0) break;// If the first element after sorting is greater than 0, Then the following elements are greater than 0, non-existent 3 The sum of the numbers is 0
if(i>0 && nums[i]==nums[i-1]) continue; // Filter duplicates
int middle=-nums[i];
// Define double pointer
int left=i+1,right=nums.length-1;
while(left<right){
if(nums[left]+nums[right]>middle){// It indicates that the positive number selected is too large , The right pointer should move to the left
right--;
}else if(nums[left]+nums[right]<middle){// It means that the negative number is too small ,left Should move to the right
left++;
}else{ //ums[left]+nums[right]==middle
list.add(new ArrayList<>(Arrays.asList(nums[left],nums[i],nums[right])));//Arrays.asList() Convert array to List aggregate
// The pointer continues to move
left++;
right--;
while(left<right && nums[left]==nums[left-1]) left++;// duplicate removal
while(left<right && nums[right]==nums[right+1]) right--;
}
}
}
return list;
}
public static void main(String[] args) {
Solution s=new Solution();
System.out.println(s.threeSum(new int[]{-1,0,1,2,-1,-4}));
}
}边栏推荐
- 线性代数行列式计算方法之降阶法
- 去广场吃饭
- 网上开通证券账户安全吗?
- Reduced order method of linear algebraic determinant calculation method
- Is the link of Huatai Securities' low commission account opening safe? How to handle low commission
- did you register the component correctly
- Complex data processing of multi subsystem and multi business modules -- project development practice based on instruction set Internet of things operating system
- OneFlow v0.8.0正式发布
- 20.ref与props
- MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
猜你喜欢
![[ar learning] - II. Environment construction](/img/e8/c20de6a46ef70b6eb49684d685e4cd.png)
[ar learning] - II. Environment construction

如何在OneFlow中新增算子

如何合理地估算线程池大小

osgearth2.8编译siverlining云效果

数组——59. 螺旋矩阵 II

New product listing | A-share floor derivatives market point

After the input error of next numerical data type () occurs, it can still be input normally next time

scanf()和getchar()的用法讨论

源启数字化:既有模式,还是开源创新?|砺夏行动

Uncover the working principle of solid state disk
随机推荐
Complex data processing of multi subsystem and multi business modules -- project development practice based on instruction set Internet of things operating system
LyScriptTools 扩展Script模块
New product listing | A-share floor derivatives market point
选择大于努力!贵阳校区小哥哥0基础成功转行软件测试收获12K!
21.mixin混入详解
AB球队得分流水表,得到连续三次得分的队员名字 和每次赶超对手的球员名字(pdd)
JDK installation package and MySQL installation package sorting
网上开通证券账户安全吗?
Model loading of assimp Library under QT
Training log on July 22, 2022
梅科尔工作室-小熊派开发笔记3
How to solve the problem that the solid state disk cannot be found when installing win11?
Win11没有Word文档怎么办?Win11没有Word文档解决教程
20. Ref and props
Mecol Studio - Little Bear Development Notes 3
2022 Shandong elderly care exhibition, China International elderly care service industry exhibition, Jinan elderly care industry exhibition
数仓4.0笔记——数仓环境搭建—— DataGrip准备和数据准备
The latest version of conflict+docker+mysql8 deployment tutorial
数组——27. 移除元素
Drools(1):Drools简介