Runtime error: last input [0, 0, 0]. BUT WHY?!


  • 0
    A
    public class Solution {
    public List<List<Integer>> threeSum(int[] S) {
        List result = new ArrayList();
        if (S.length < 3) 
            ;
        else {
            Arrays.sort(S);
            if (S.length == 3 && S[0]+S[1]+S[2] == 0) 
                result.add(Arrays.asList(S));
            else {
                int i = 0;
                while (true) {
                    int l = i+1;
                    int r = S.length-1;
                    while (true) {
                        int sum = S[i]+S[l]+S[r];
                        if (sum > 0) 
                            while (l < --r && S[r] == S[r+1])
                                ;
                        else {
                            if (sum == 0)
                                result.add(Arrays.asList(new int[]{S[i], S[l], S[r]}));
                            while (++l < r && S[l] == S[l-1])
                                ;
                        }
                        
                        if (l == r)
                            break;
                    }
                    
                    while (++i < S.length-2 && S[i] == S[i-1])
                        ;
                    if (i == S.length-2)
                        break;
                }
            }
        }
        return result;
    }
    

    }


Log in to reply
 

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