Pascal's Triangle Solution


  • 0
    I
    class Solution {
        //C(line,i) = line! / (line-i)! * i!
        //C(line,i-1) = line! / (line-i+1)! * (i-1)!
        // C(line,i) / C(line,i-1) = (line - (i-1))! (i-1)! / (line-i)! i * (i-1)!
        // C(line,i) / C(line,i-1) = (line -i + 1) / i
        // C(line, i) = C(line, i-1) * (line -i + 1) / i
        
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> result = new ArrayList<>();
            for(int i=0;i<numRows;i++){
                List<Integer> lineEntries = new ArrayList<>();
                int C = 1; //Since first entry is always 1
                lineEntries.add(C);
                for(int j=1;j<=i;j++){
                    C = C * (i - j + 1)/j;
                    lineEntries.add(C);
                }
                result.add(lineEntries);
            }
            return result;
        }
    }
    
    

Log in to reply
 

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