public List<List<Integer>> threeSum(int[] nums) {
if(nums.length < 3) return new ArrayList();
List<List<Integer>> res = new LinkedList();
HashMap<List, Boolean> hm = new HashMap();
Arrays.sort(nums);
for(int i = 0; i < nums.length  2; i++){
int start = i + 1, end = nums.length  1;
while(start < end){
while(start < end && nums[i] + nums[start] + nums[end] < 0)
start++;
while(start < end && nums[i] + nums[start] + nums[end] > 0)
end ;
if(start < end && nums[i] + nums[start] + nums[end] == 0){
List<Integer> arr = new ArrayList(Arrays.asList(nums[i], nums[start], nums[end]));
if(!hm.containsKey(arr)){
res.add(arr);
hm.put(arr, true);
}
start ++;
end ;
}
}
}
return res;
Does any one tell me why it sometimes got TLE and sometimes AC?


3 situations to consider:
1)nums[end],num[end1],nums[end2] ... nums[endn]. If all equals to same value, then you have to skip all them and go nums[endn1] directly.
2)Same thing to nums[start]
3)if nums[start]>0, or nums[end]<0, then no need to run on.
Wasted me a long time on this. I think leetcode is just going into a wrong direction on this TLE thing.