Why? my code cannot pass all cases.. I thought this question is easy..


  • 0
    W
    public class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
    	List<List<Integer>> result = new ArrayList<List<Integer>>();
    	Arrays.sort(nums);
    	int begin = 0;
    	int end = nums.length-1;
    	int mid = 0;
    	int value = 0;
    	while(begin<end){
    		
    		mid = begin+1;
    		end = nums.length-1;
    		while(mid<end){
    	    List<Integer> curr = new ArrayList<Integer>();
    		value = nums[begin] + nums[end] + nums[mid];
    		if (value==0){
    			curr.add(nums[begin]);
    			curr.add(nums[mid]);
    			curr.add(nums[end]);
        	if (!result.contains(curr)){
    			result.add(curr);}
    			end-=1;
    			mid+=1;
    			
    		}			
    		if(value>0){
    			end-=1;
    		}
    		if(value<0){
    			mid+=1;
    		}
    
    		
    		}
    		begin+=1;
    		
    	}
    	
    	
    	return result;
        
    }
    

    }


Log in to reply
 

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