Printed out right in eclipse but got wrong answer in leetcode unit test


  • 0
    C
    public class PascalsTriangle {
    public static List<List<Integer>> generate(int numRows) {
    	int j = 0;
    	List<List<Integer>> result = new ArrayList<List<Integer>>();
    	if (numRows == 0) return result;
    	while(j < numRows){
    		List<Integer> list = new ArrayList<Integer>();
    		int i = 0;
    		if (j == 0){
    			list.add(0, 1);
    			result.add(j, list);
    		}
    		else{
    			while(i < j + 1){	// j + 1 is the length of list
    				if (i == 0 || i == j) list.add(i, 1);
    				else{
    					int l = result.get(j - 1).get(i - 1);
    					int r = result.get(j - 1).get(i);
    					list.add(i, l + r);
    				}
    				result.add(j, list);
    				i++;
    			}
    		}
    		j++;
    	}
    	return result;
    }
    
    public static void main(String[] args) {
    	List<List<Integer>> result = generate(2);
    	for (int j = 0; j < 2; j++) {
    		for (int i = 0;  i < j + 1; i++) {
    			if (i == j) System.out.print(result.get(j).get(i));
    			else System.out.print(result.get(j).get(i) + ",");
    		}
    		System.out.println();
    	}
      }
    }
    

    I got the correct result which is {1}, {1,1} in eclipse when I printed it out.
    But when it went through leetcode unit test, it returned a wrong answer which is {1}, {1,1}, {1,1}. Can anyone tell me why?


  • 0
    Q

    Here is my accepted solution:

    public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        
        if(numRows == 0)
            return res;
        
        // Add the first array [1]
        List<Integer> first = new ArrayList<Integer>();
        first.add(1);
        res.add(first);
    
        // We can return now if 1
        if(numRows == 1) {
            return res;
        }
    
        // Starting from 2nd array:
        // 1, First and last are 1
        // 2, Everyone in the middle is a sum from previous array
        for(int i = 1; i < numRows; i++) {
            List<Integer> each = new ArrayList<Integer>();
            each.add(1); // add first
            for(int j = 0; j < i - 1; j++) { // every one except first and last
                each.add(res.get(i - 1).get(j) + res.get(i - 1).get(j + 1));
            }
            each.add(1); // add last
            // Current array is completed
            res.add(each);
        }
        
        return res;
    }
    }

  • 3
    H

    Put "result.add(j, list); " out of the inner while loop.
    Finish the level and then add the list to the result, otherwise you add multiple times.

    And the reason why you get the "right" answer in eclipse is:
    in your main function, you only loop through the first and second level, sure you'll get only two levels even though your result contains three levels...


Log in to reply
 

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