zoukankan      html  css  js  c++  java
  • 15.三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

    注意:答案中不可以包含重复的三元组。

    例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
    
    满足要求的三元组集合为:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]
     1     //方法一:暴力法,超时未通过
     2     public List<List<Integer>> threeSum (int[] nums) {
     3         int len = nums.length;
     4         //List<Integer> innerList = new ArrayList<>(); innerList是一个引用数据类型,如果若直接添加,会导致前面的值改变
     5         ArrayList<List<Integer>> outerList = new ArrayList<>();
     6         for(int i=0; i<len-2; i++) {
     7             for(int j=i+1; j<len-1; j++) {
     8                 for(int k=j+1; k<len; k++) {
     9                     if(nums[k] == (-(nums[i] + nums[j]))) {            
    10                         Integer[] arr = new Integer[] {nums[i], nums[j], nums[k]};
    11                         Arrays.sort(arr);
    12                         if(outerList.isEmpty()) {
    13                             outerList.add(Arrays.asList(arr));
    14                         }
    15                         ListIterator<List<Integer>> lit = outerList.listIterator();
    16                         boolean flag = true;
    17                         while(lit.hasNext()) {
    18                             if(lit.next().equals(Arrays.asList(arr))) {
    19                                 flag = false;
    20                             }
    21                         }
    22                         if(flag) {                            
    23                             lit.add(Arrays.asList(arr));
    24                         }
    25                     }
    26                 }
    27             }
    28         }        
    29         return outerList;
    30     }
    31     
    32     public List<List<Integer>> threeSum2(int[] nums) {
    33         List<List<Integer>> result = new ArrayList<>();
    34         if(nums.length == 0) {
    35             return result;
    36         }
    37         Arrays.sort(nums);
    38         int len = nums.length;
    39         for(int i=0; i<len; i++) {
    40             //第一次保证起始值不会重复,如[-2,-2,0,2]
    41             if((i>0) && (nums[i] == nums[i-1])) {
    42                 continue;
    43             }
    44             int right = len - 1;
    45             int left = i+1;
    46             while(left < right) {
    47                 //第二次保证后面两数不会重复,如[-2,0,0,0,2,2]
    48                 if((left>(i+1)) && (nums[left] == nums[left-1])) {
    49                     left++;
    50                     continue;
    51                 }
    52                 int temp = nums[left] + nums[right];
    53                 if(temp == -nums[i]) {
    54                     result.add(Arrays.asList(new Integer[] {nums[i], nums[left], nums[right]}));
    55                     left++;
    56                     right--;
    57                 }else if(temp > -nums[i]) {
    58                     right--;                    
    59                 }else {
    60                     left++;
    61                 }
    62             }
    63         }
    64         return result;    
    65     }
    无论有多困难,都坚强的抬头挺胸,人生是一场醒悟,不要昨天,不要明天,只要今天。不一样的你我,不一样的心态,不一样的人生,顺其自然吧
  • 相关阅读:
    怎样把.git版本控制文件夹放在项目目录下
    mybatis3-generator-plugin插件地址
    @RestController
    <mvc:annotation-driven />注解意义
    关于Spring中的<context:annotation-config/>配置
    Jeecg-Boot前后端分离,针对敏感数据,加密传递方案
    微信开发SDK支持小程序 ,Jeewx-Api 1.3.1 版本发布
    Mybatis传递多个参数的4种方式(干货)
    Online开发初体验——Jeecg-Boot 在线配置图表
    JAVA开源微信管家平台——JeeWx捷微V3.3版本发布(支持微信公众号,微信企业号,支付窗)
  • 原文地址:https://www.cnblogs.com/xiyangchen/p/10846298.html
Copyright © 2011-2022 走看看