首页 > 编程语言 >算法—查找三数相加为零的三元组

算法—查找三数相加为零的三元组

时间:2023-01-06 13:36:13浏览次数:39  
标签:right nums int 相加 三元组 length 查找 result left


package algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组
* <p>
* 注意:答案中不可以包含重复的三元组。
* 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:
* [
* [-1, 0, 1],
* [-1, -1, 2]
* ]
* 结论: test()没有去重的情况下,耗时是calculate()的9.4倍,数据越多越明显。
*
*
* @author dingwen
* 2021.06.18 09:20
*/
public class Algoriehm01 {
private static int[] nums = {-1, 0, 1, 2, -1, -4, -8, 8, 6, 36, -56, -41, -10, 10, 4, 3, -3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22,-50,-51,-52,-53,-54,-55,-56,-57,-58,-59,-60,52,53,54,55,56,57,58,59,60};

public static void main(String[] args) {
viewTime(true,"calculate");
viewTime(false,"test");
}

public static List<List<Integer>> calculate() {

List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length < 3) {
return result;
}

// 双轴快排 实现
Arrays.sort(nums);

for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int left = 0;
int right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (i == left || i == right) {
break;
}
if ((num + sum) < 0) {
left++;
}
if ((num + sum) > 0) {
right--;
}
if ((num + sum) == 0) {
result.add(Arrays.asList(nums[left], nums[i], nums[right]));
break;
}
}
}

return result;
}

public static List<List<Integer>> test() {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
for (int k = 0; k < nums.length; k++) {
if ((nums[i] + nums[j] + nums[k] == 0)) {
//todo 重复值处理
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
}
}
}
}
return result;
}

public static void viewTime(boolean flag,String title) {
long start = System.nanoTime();
if (flag) {
System.out.println("calculate() = " + calculate());
} else {
System.out.println("test() = " + test());
}
long end = System.nanoTime();
System.out.println(title + "耗时 = " + (end - start));
}
}


标签:right,nums,int,相加,三元组,length,查找,result,left
From: https://blog.51cto.com/u_15932195/5993023

相关文章

  • 查找所有树的直径都经过的边的数量
    P3304目录大体思路code此题思路上跟https://www.cnblogs.com/kingbuffalo/p/17027323.html上的思路差不多。目录大体思路第一遍dfs找到最远点第二遍dfs找到直......
  • Unity中使用GameObject.Find、Transform.Find查找GameObject
    ​GameObjectFindTransformFind查找游戏对象​​​前置条件​​​相关API​​​1GameObjectFind​​​​2TransformFind​​​​3其他查找​​​​实际测试​​​​即使......
  • 2. 两数相加
    问题链接https://leetcode.cn/problems/add-two-numbers/description/解题思路这题是倒着存储的,也要求我们返回一个倒着的链表。即它需要我们左对齐,相加之后向后进位,但......
  • BST查找结构与折半查找方法的实现与实验比较
    简介作业:查找结构与排序方法作业题目:BST查找结构与折半查找方法的实现与实验比较要求编写程序实现BST存储结构的建立(插入)、删除、查找和排序算法;实现折半查找算法......
  • 蓝桥杯——查找的妙趣
    一、查找1.1递归式二分查找作为查找的必学算法,二分查找大家一定不陌生,通过前面我们所学的递归,那么我们继续强化递归思想,将二分查找转换成递归的方式。任何循环都能改......
  • 一个查找mysql数据库无主键表的脚本
    说明:遍历所有的库表然后查询是否具有主键/bin/bashdb_host=172.19.211.2#dbipdb_name_list="chimessoxrayintcommpultus"#填写db_name支持多个数据库,以空格隔......
  • c++ 查找目录下的子目录及文件
    c++读取指定目录下的所有目录名称+文件名称-远征i-博客园(cnblogs.com) 文件句柄的类型long如果不行试试longlong 另外:使用了批处理,这篇很好CMD批处理循环......
  • Java8中比较器和收集器的常用示例-排序、流转集合、分组、分区、查找最大最小值
    场景Java8新特性-Stream对集合进行操作的常用API:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126070657前面讲Stream的常用api。下面讲比较器和收集器......
  • MongoDB时间范围查找
    1.查询时间范围,且模糊匹配message列中含“api”字符串的记录db.collection_fee.find({'time':{$gte:ISODate("2023-01-05T09:58:51.122Z"),$lte:ISODate("2......
  • 查找php-fpm
    [root@VM-4-6-centos/]#find/-namephp-fpm/opt/remi/php74/root/usr/sbin/php-fpm/etc/opt/remi/php74/sysconfig/php-fpm/var/opt/remi/php74/log/php-fpm/var/opt/r......