My answer is..


  • 0
    F

    PROBLEM OBJECTIVE
    UP AND DOWN
    DUPLICATION PROBLEM - HASH

    public class Solution {
        private final static int totalRotations = 3;
        private final static int totalAnswer = 0;
        private List<List<Integer>> outputList = new ArrayList<List<Integer>>();
        private List<String> hashList = new ArrayList<String>();
     
        public List<List<Integer>> threeSum(int[] nums) {
        	if (nums.length < totalRotations) return new ArrayList<List<Integer>>();
            Arrays.sort(nums);
            
            List<Integer> numberList = minimizeNumberCounts(nums);
            nums = listToArray(numberList);
    
            boolean hasZero = checkZero(numberList);
            int[] mnums = getMinusNums(nums);
            int[] pnums = getPlusNums(nums);
    
            find(hasZero, mnums, pnums);
            find(mnums, pnums);
            find(pnums, mnums);
    
            //printList();
            return outputList;
        }
    
    	private void find(boolean hasZero, int[] mnums, int[] pnums) {
    		if (hasZero) {
    	        int[] form = new int[totalRotations];
    	        form[0] = 0;
    			for (int mnum : mnums) {
    	        	form[1]=mnum;
    	        	for (int i=0; i<pnums.length; i++) {
    	        		form[2] = pnums[i];
    	            	if (form[1] + form[2] == totalAnswer) {
    	                	int[] clone = form.clone();
    						Arrays.sort(clone);
    	                	String hash = makeHash(clone);
    						if (hasDuplicateAnswer(hash)) break;;
    						ArrayList<Integer> arrayList = arrayToList(clone);
    						hashList.add(hash);
    						outputList.add(arrayList);
    	            	}
    	        	}
    	        }
            }
    	}
    
    	private void find(int[] nums, int[] othernums) {
            int[] form = new int[totalRotations];
    		for (int num : nums) {
            	form[0]=num;
            	for (int i=0; i<othernums.length; i++) {
            		form[1] = othernums[i];
                    int [] clonenums = Arrays.copyOfRange(othernums, i+1, othernums.length);
                    for (int j=clonenums.length-1; j>=0; j--) {
                    	form[2] = clonenums[j];
                    	if (form[0] + form[1] + form[2] == totalAnswer) {
                        	int[] clone = form.clone();
        					Arrays.sort(clone);
                        	String hash = makeHash(clone);
        					if (hasDuplicateAnswer(hash)) break;;
        					ArrayList<Integer> arrayList = arrayToList(clone);
        					hashList.add(hash);
        					outputList.add(arrayList);
                    	}
                    }
            	}
            }
    	}
    
    	private int[] getPlusNums(int[] nums) {
            int index=0;
            for (int num : nums) {
            	if (num>0) {
            		break;
            	}
            	index++;
            }
    
            return Arrays.copyOfRange(nums, index, nums.length);
    	}
    
    	private int[] getMinusNums(int[] nums) {
    		int index=0;
            for (int num : nums) {
            	if (num >= 0) {
            		break;
            	}
            	index++;
            }
    
            return Arrays.copyOfRange(nums, 0, index);
    	}
    
    	private boolean checkZero(List<Integer> ll) {
    		int zeroCount=0;
            boolean hasZero = false;
            for (int i=0; i<ll.size(); i++) {
            	if(ll.get(i)==0) {
            		zeroCount++;
            		if (zeroCount ==3)
                    	outputList.add(arrayToList(new int[]{0,0,0}));
            		hasZero = true;
            		continue;
            	}
            }
    		return hasZero;
    	}
    
    	private List<Integer> minimizeNumberCounts(int[] nums) {
            List<Integer> ll = new ArrayList<Integer>();
            int previousCount = 0;
            int length = nums.length;
    		int maxInt = nums[length-1];
            int minInt = nums[0];
            Integer temp = null;
            for (int num : nums) {
            	if(temp != null && temp == num && num!=0) previousCount++;
            	else previousCount=0;
    
            	if(previousCount <1) ll.add(num);
            	else if (previousCount <2) {
            		if (num>0 && Math.abs(minInt) >= num*2) ll.add(num);
            		else if (num<0 && maxInt >= Math.abs(num)*2) ll.add(num);
            	}
            	temp=num;
            }
            return ll;
    	}
    
        private void printList() {
        	int index = 0;
    		for(List<Integer> list : outputList) {
    			System.out.print(index + " : ");
    			System.out.println(list);
    			index++;
    		}
    	}
    
    	private String makeHash(int[] memory) {
    		StringBuilder sb = new StringBuilder();
    		for (int num : memory) {
    			sb.append(num+"");
    		}
    		return sb.toString();
    	}
    
    	private ArrayList<Integer> arrayToList(int[] memory) {
    		List<Integer> arrayList = new ArrayList<Integer>();
    		for (int num : memory) {
    			arrayList.add(num);
    		}
    		return (ArrayList<Integer>) arrayList;
    	}
    
    	private int[] listToArray(List<Integer> ll) {
    		int[] nums;
    		nums = new int[ll.size()];
            for (int i=0; i<ll.size(); i++) {
            	nums[i] = ll.get(i);
            }
    		return nums;
    	}
    
    	private boolean hasDuplicateAnswer(String string) {
    		if (hashList.contains(string)) return true;
    		else return false;
    	}
    }
    

Log in to reply
 

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