【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 }
时间: 2024-10-25 19:06:48

【leetcode】15. 3Sum的相关文章

【LeetCode】16. 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【LeetCode】016 3Sum Closest

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-

【LeetCode】 16. 3Sum Closest 解题小结

题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1

【LeetCode】015 3Sum

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [

【LeeCode】 15. 3Sum 解题小结

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Candy

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies