Java solution based on mathematical definition


  • 0
    N
    public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> ele = new ArrayList<Integer>();
        ele.add(1);
         int m = rowIndex;
         int pre = 1;
         for(int j = 1; j <= rowIndex; j++) {
              int current = (int)Math.rint((double)pre / j * (m--));
              ele.add(current);
              pre = current;
        } 
        return ele;
    }    
    }
    

    element for nth row should be(C(n,0), C(n,1)....C(n, n-1), C(n,n));
    it is the coefficients of (a+b)^n


Log in to reply
 

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