Java 1ms solution hard uncommon way


  • 0
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> line = new ArrayList<Integer>();
        long num = 1;
        for(int i = 0; i < numRows; i++){
            for (int j = 0; j <= i; j++){
                line.add( (int)num );
                num = num * (i - j) / ( j + 1 );
            }
            num = 1;
            result.add(line);
            line = new ArrayList<Integer>();
            //line.clear();  why this does't work?
        }
        return result;
    }
    

    Why line.clear() not working?
    And any chance to improve?


Log in to reply
 

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