zoukankan      html  css  js  c++  java
  • 【leetcode】15. 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.

    解题分析:

    这道题注意一下几点即可:

    1,先固定前两个数的位置,移动第三个数,这样的查找方式不会有数字组合的遗漏;

    2,要考虑到数组元素有重复的情况下的处理。

    3,若先为数组排序(这类题一般都这么做),要充分利用有序的特点来减少比较情况,优化代码。

    具体代码:

     1 public class Solution {
     2    public static List<List<Integer>> threeSum(int[] nums) {
     3         List<List<Integer>> results = new ArrayList<List<Integer>>();
     4         //边界情况的判断
     5         if(nums.length<3){
     6             //results.add(new ArrayList<Integer>());
     7             return results;
     8         }
     9         if(nums.length==3){
    10             if(nums[0]+nums[1]+nums[2]==0){
    11                 List<Integer> array =new ArrayList<Integer>();
    12                 array.add(nums[0]);
    13                 array.add(nums[1]);
    14                 array.add(nums[2]);
    15                 results.add(array);
    16                 return results;
    17             }
    18             else{
    19                 //results.add(new ArrayList<Integer>());
    20                 return results;
    21             }
    22         }
    23         //先为数组排序
    24         Arrays.sort(nums);
    25         //先把前两个数确定,变第三个数得值,以保证查找了所有例子
    26         for(int i=0;i<nums.length-2;i++){
    27             //如果第一个数已经大于零,就没有必要再找下去了
    28             if(nums[i]>0)
    29             break;
    30             for(int j=i+1;j<nums.length-1;j++){
    31                 //同上
    32                 if(nums[i]+nums[j]>0)
    33                 break;
    34                 for(int index=j+1;index<nums.length;index++){
    35                     if(nums[i]+nums[j]+nums[index]==0){
    36                         List<Integer> array = new ArrayList<Integer>();
    37                         array.add(nums[i]);
    38                         array.add(nums[j]);
    39                         array.add(nums[index]);
    40                         results.add(array);
    41                         //避免结果重复的处理
    42                         while(index+1<nums.length && nums[index+1]==nums[index]){
    43                             index++;
    44                         }
    45                     }
    46                         
    47                 }
    48                 //避免结果重复的处理
    49                 while(j+1<nums.length-1 && nums[j+1]==nums[j]){
    50                     j++;
    51                 }
    52                 
    53             }
    54             //避免结果重复的处理
    55             while(i+1<nums.length-2 && nums[i+1]==nums[i]){
    56                 i++;
    57             }
    58         }
    59         return results;
    60         
    61     }
    62 }
  • 相关阅读:
    MySQL-基本sql命令
    Java for LeetCode 203 Remove Linked List Elements
    Java for LeetCode 202 Happy Number
    Java for LeetCode 201 Bitwise AND of Numbers Range
    Java for LeetCode 200 Number of Islands
    Java for LeetCode 199 Binary Tree Right Side View
    Java for LeetCode 198 House Robber
    Java for LeetCode 191 Number of 1 Bits
    Java for LeetCode 190 Reverse Bits
    Java for LeetCode 189 Rotate Array
  • 原文地址:https://www.cnblogs.com/godlei/p/5636460.html
Copyright © 2011-2022 走看看