编写Java程序以查找所有的三元组,使得所有三个元素[x,y,z]的总和等于指定的数,三元组不能有重复(不考虑元素顺序)

29 阅读1分钟

编写Java程序以查找所有的三元组,使得所有三个元素[x,y,z]的总和等于指定的数,三元组不能有重复(不考虑元素顺序)。例如样本数组:[1,-2,0,5,-1,-4] ,指定的数为 2,则输出结果为[1,5,-4]和[-2, 5, -1]。

import java.util.ArrayList;
import java.util.List;
public class Exercise36 {
    public static void main(String[] args) {
        int[] input = {1, -2, 0, 5, -1, -4};
       int target = 2;
       Exercise36 r = new Exercise36();
        System.out.println(r.threeSum(input,target));      
    }
    public List<List<Integer>> threeSum(int[] nums, int target) {
        List<List<Integer>> my_List = new ArrayList<List<Integer>>();
        for(int i = 0; i < nums.length; i++){
            for(int j = i; j < nums.length ;j++){
                for(int k = j; k<nums.length;k++){
                    if ( i != j && j != k && i != k && (nums[i] + nums[j] + nums[k] == target)){
                        List<Integer> inner_List = new ArrayList<Integer>(3);
                        inner_List.add(nums[i]);
                        inner_List.add(nums[j]);
                        inner_List.add(nums[k]);
                        my_List.add(inner_List);
                    }
                }
            }
        }
       return my_List;
    }
  }