绝对高效实现! 给你一个包含n个整数的数组nums,判断nums是否存在三个元素a,b,c使得a+b+c =0...

所有实现代码均使用JAVA

letcode上面有个算法题,贴原图
**在这里插入图片描述**

先分析一下:

第一个条件,a+b+c = 0,这个好办,最简单的就是循环三次,把所有情况都过一篇就可得到所有结果,
第二个条件,满足条件且不重复,这个的话将每次符合条件的三个数字存进一个Map<Integer,Integer>,然后再存进List<Map<Integer,Integer>>,每次遇到符合条件的然后遍历List,再通过map.get()判断是否存在重复即可,
至此,完结,

但是如果nums的个数无穷大,那么循环次数也会指数级增长,所以上面这种做法不可取,

重新分析一下:

我们可以先拆分来看,从 a+b+c = 0 可以得出 a = 0 -(b + c)
a就是最外层每次循环的当前值
然后就可以搭第一层循环了,

int[] nums = {-1, 0, 1, 2, -1, -4}
List<Integer[]> resList = new ArrayList<>(); //保存结果集合

for (int i = 0; i < nums.length; i++) {
    int a = nums[i];
    int bAndC = 0 - nums[i];// b+c

   
 }

然后再来分析b+ c; b+c就是用除了a之外的剩余数组成的
可以再搭一个循环获取b + c的值,但是需要一个Map来辅助, 这样的话一次遍历就可以把两个数全部“揪”出来,

int[] nums = {-1, 0, 1, 2, -1, -4};
List<Integer[]> resList = new  ArrayList<>();  //保存结果集合
for (int i = 0; i < nums.length; i++) {
      int a = nums[i];
      int bAndC = 0 - nums[i];// b+c
      Map<Integer,Integer> map = new HashMap<>();  //保存判断结果值b、c的值
      for (int j = i + 1; j < nums.length; j++) {
       if (map.containsKey(bAndC  - nums[j])) {
          resList .add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]});          
       } else {
        map.put(nums[j], nums[j]);
       }
    }  
 }

这样下来所有的数组都就可以找到了,

分析最后一个问题,不重复,

再来分析 a + b + c = 0,

a + b + c = 0 可得 a + b = 0 – c,即通过a + b 确定 c 的值

符合的三元数组一共有三个数,总共有三种组合,

a + b = 0 - c,

b + c = 0 - a ,

a + c = 0 - b,

所以,遇到符合的数组就把当前所有情况保存起来,然后再遇到符合数组时再判断是否有相同的,这需要一个HashSet辅助,最后就可以获取预期结果了

int[] nums = {-1, 0, 1, 2, -1, -4};
List<Integer[]> resList = new ArrayList<>();  //保存结果集合
HashSet noRepeatSet = new HashSet();  //保存不重复的值并判重

for (int i = 0; i < nums.length; i++) {
    int a = nums[i];
    int bAndC = 0 - nums[i];// b+c
    Map<Integer, Integer> map = new HashMap<>();   //保存判断结果值b、c的值
    for (int j = i + 1; j < nums.length; j++) {
        if (map.containsKey(bAndC  - nums[j])) {
            if (!noRepeatSet.contains(nums[i] + nums[j])) {  //判重
                resList.add(new Integer[]{map.get(bAndC  - nums[j]), nums[j], nums[i]});
                
                noRepeatSet.add(map.get(bAndC  - nums[j]) + nums[j]);  //保存所有可能值
                noRepeatSet.add(map.get(bAndC  - nums[j]) + nums[i]);
                noRepeatSet.add(nums[i] + nums[j]);
            }
        } else {
            map.put(nums[j], nums[j]);
        }
    }
}

但这样还有问题,
-2 + 2 = 0
-3 + 3 = 0

a + b 会出现相同结果然而内容不同,所以这样不能判定,

两个不行,那就三个(全部)吧,

int[] nums = {-1, 0, 1, 2, -1, -4, 0, 0,4};
List<Integer[]> resList = new ArrayList<>();  //保存结果集合
HashSet noRepeatSet = new HashSet();  //保存不重复的值并判重
int[] curArr;


for (int i = 0; i < nums.length; i++) {
    int a = nums[i];
    int bAndC = 0 - nums[i];// b+c
    Map<Integer, Integer> map = new HashMap<>();  //保存判断结果值b、c的值
    for (int j = i + 1; j < nums.length; j++) {
        if (map.containsKey(bAndC  - nums[j])) {
            curArr = new int[]{map.get(bAndC  - nums[j]), nums[j], nums[i]};
            Arrays.sort(curArr);  //排序

            if (!noRepeatSet.contains(curArr[0]+""+curArr[1]+""+curArr[2])) {  //判重
                resList.add(new Integer[]{map.get(bAndC  - nums[j]), nums[j], nums[i]});
                noRepeatSet.add(curArr[0]+""+curArr[1]+""+curArr[2]);  //保存此次数组值
            }
        } else {
            map.put(nums[j], nums[j]);
        }
    }
}

符合条件的的先排序再做判断,这样就可以准确判重了.
.
.
.

但是还有这样一种特殊情况,存在多个0的时候,结果里并不会出现 [0,0,0]

int[] nums = {-1, 0, 1, 2, -1, -4,0,0};

所以把这种情况加进去

int[] nums = {-1, 0, 1, 2, -1, -4, 0, 0,4};
List<Integer[]> resList = new ArrayList<>();  //保存结果集合
HashSet noRepeatSet = new HashSet();  //保存不重复的值并判重
boolean isAllO = false;  //是否出现过都为0
int[] curArr;
int k = 0;

for (int i = 0; i < nums.length; i++) {
    int a = nums[i];
    int bAndC = 0 - nums[i];// b+c
    Map<Integer, Integer> map = new HashMap<>();  //保存判断结果值b、c的值
    for (int j = i + 1; j < nums.length; j++) {
        k++;
        if (map.containsKey(bAndC - nums[j])) {
            if (bAndC == 0 && nums[i] == 0 && nums[j] == 0) {  //已添加过跳过本轮
                if (isAllO) {
                    continue;
                } else {
                    resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]});  //全为 0 添加到集合
                    isAllO = true;
                    continue;
                }
            }
            curArr = new int[]{map.get(bAndC - nums[j]), nums[j], nums[i]};
            Arrays.sort(curArr);  //排序

            if (!noRepeatSet.contains(curArr[0]+""+curArr[1]+""+curArr[2])) {  //判重
                resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]});
                noRepeatSet.add(curArr[0]+""+curArr[1]+""+curArr[2]);  //保存此次数组值
            }
        } else {
            map.put(nums[j], nums[j]);
        }
    }
}

以上所有Map和hashSet都可互换,但是使用HashSet比较“应景”一点。
以上所有解决方法均为个人理解,如有更好方案还望留言

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值