Takes different approach and beats 90%


  • 0
    L

    It takes a different approach than others and uses Pascal's theorem to formulate the answer.

        public List<Integer> getRow(int rowIndex) {
            List<Integer> rows = new ArrayList(rowIndex+1);
            for(int i=0;i<rowIndex+1;i++) {
                rows.add(ncr(rowIndex,i));
            }
            return rows;
        }
        
      
        private int ncr(int n, int k) {
            int m= Math.min(n-k, k);
            long cnm= 1;
     
            for (int i= n-m+1, j= 1; i <= n; i++, j++) {
                cnm*= i;
                cnm/= j;
            }
     
            return (int)cnm;
        }
    

Log in to reply
 

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