leetcode15

leetcode15_第1张图片

代码:

package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class P15_dis {

	public static void main(String[] args) {
		//int[] nums = {-1, 0 ,0,1,1, 2, -1, -4};
		int[] nums={0,0,0};
		System.out.println(threeSum(nums));

	}
	

public static List> threeSum(int[] nums) {
        
		List> result=new ArrayList>();	
        if(nums.length<3)
            return result;
        if(nums[0]==0 &&nums[1]==0 && nums[2]==0){
			result.add(Arrays.asList(0,0,0));
			return result;
		}
		Arrays.sort(nums);
		for(int i=0;i0 && nums[i]!=nums[i-1]) ){					//去掉可能重复的值,因为排序后,相等的值必在一起,而他们会导致重复
				int j=i+1,k=nums.length-1;
				while(j0){
						//while(j each=new ArrayList();
						/*boolean flag=true;
						Iterator> iterator=result.iterator();					
						while(iterator.hasNext()){
							List iter=iterator.next();
							//if(iterator.next().get(0)==nums[i] && iterator.next().get(1)==nums[j] &&iterator.next().get(2)==nums[k]){
							if(iter.get(0)==nums[i] && iter.get(1)==nums[j] &&iter.get(2)==nums[k]){
								flag=!flag;
								break;
							}
						}
						if(flag){*/
							each.add(nums[i]);each.add(nums[j]);each.add(nums[k]);
							result.add(each);
//						}
						while(j

 

你可能感兴趣的:(编程题)