# My clean O(k) java solution

• Based on rules:

row k of Pascal's Triangle:

`[C(k,0), C(k,1), ..., C(k, k-1), C(k, k)]`

and

`C[k,i] = C[k,i-1]*(k-i+1)/i`

``````    public class Solution {
public List<Integer> getRow(int rowIndex) {
Integer[] rowList = new Integer[rowIndex+1];
rowList[0] = 1;
for(int i=1; i<rowList.length;i++) {
rowList[i] = (int)((long)rowList[i-1]*(rowIndex-(i-1))/(i));
}
return Arrays.asList(rowList);
}
}``````

• Nice solution

• This post is deleted!

• It is the most straightforward way I believe

• @joenjoin
Amazing solution! But how you know the rules, by observation or you learned it from somewhere else?

• @mirocody I might have learnt this rule when I was solving some other problems.

• A mathematics way!!!!
it‘s brilliant !
How do you come up with this ?

• Formula is available on Wiki:
Calculating a row or diagonal by itself

• This post is deleted!

• This post is deleted!

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