My too slow answer


  • 0
    I
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Solution {
    	public List<List<Integer>> threeSum(int[] nums) {
    		Arrays.sort(nums);
    		List<List<Integer>> result = new ArrayList<>();
    		if (nums==null || nums.length<3) return result;
    		if (nums[0]==0 && nums[1]==0 && nums[2]==0) {
    			List<Integer> list=new ArrayList<>();
    			list.add(0);
    			list.add(0);
    			list.add(0);
    			result.add(list);
    			return result;
    		}
    		if (nums[0]>=0 || nums[nums.length-1]<=0) return result;
    
    		int tPoint=0;
    		int cntZero=0;
    		for (int i = 1; i < nums.length; i++) {
    			if(nums[i]==0){
    				cntZero++;
    			}
    			if(nums[i]>0) {
    				tPoint=i;break;
    			}
    		}
    		if(cntZero>=3){
    			List<Integer> list=new ArrayList<>();
    			list.add(0);
    			list.add(0);
    			list.add(0);
    			result.add(list);
    		}
    
    		int choosei=nums[nums.length-1]+1;
    		int choosej=nums[nums.length-1]+1;
    		int choosek=nums[0]-1;
    		for (int i = 0; i < tPoint; i++) {
    			if(choosei==nums[i]) continue;
    			choosej=nums[nums.length-1]+1;
    			for (int j = tPoint; j < nums.length-1; j++) {
    				if(choosej==nums[j]) continue;
    				boolean isSame=false;
    				int choose=nums[i];
    				choosek=nums[0]-1;
    				for (int k = j+1; k < nums.length; k++) {
    					if(choosek==nums[k]) continue;
    					if(nums[j]==nums[k]) {
    						if(isSame==false){
    							isSame=true;
    						}else{
    							j++;
    							continue;
    						}
    					}
    					if(choose!=nums[k] && nums[i]+nums[j]+nums[k]==0) {
    						choose=nums[k];
    						choosei=nums[i];
    						choosej=nums[j];
    						choosek=nums[k];
    							List<Integer> list=new ArrayList<>();
    							list.add(nums[i]);
    							list.add(nums[j]);
    							list.add(nums[k]);
    							result.add(list);
    					}
    				}
    			}
    		}
    
    		choosei=nums[nums.length-1]+1;
    		choosej=nums[nums.length-1]+1;
    		choosek=nums[0]-1;
    		for (int i = 0; i < tPoint-1; i++) {
    			if(choosei==nums[i]) continue;
    			choosej=nums[nums.length-1]+1;
    			boolean isSame=false;
    			for (int j = i+1; j < tPoint; j++) {
    				if(choosej==nums[j]) continue;
    				if(nums[i]==nums[j]) {
    					if(isSame==false){
    						isSame=true;
    					}else{
    						i++;
    						continue;
    					}
    				}
    				choosek=nums[0]-1;
    				for (int k = tPoint; k < nums.length; k++) {
    					if(choosek==nums[k]) continue;
    					if(nums[i]+nums[j]+nums[k]==0) {
    						choosei=nums[i];
    						choosej=nums[j];
    						choosek=nums[k];
    							List<Integer> list=new ArrayList<>();
    							list.add(nums[i]);
    							list.add(nums[j]);
    							list.add(nums[k]);
    							result.add(list);
    					}
    				}
    			}
    		}
    
    		return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.