DEV Community

duccanhole
duccanhole

Posted on

code every day with me

--DAY 16--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:

  • Problem: Pascal's Triangle II (read more about it here)
  • Detail: here
  • My solution (javascript):
var getRow = function(index) {
    if(index==0) return [1];
    if(index==1) return [1,1];
    let ans=[[1],[1,1]];
    for(let i=2;i<=index;i++){
        let tmp=[];
        tmp[0]=1;
        for(let j=1;j<i+1;j++){
            tmp[j]=ans[i-1][j-1]+ans[i-1][j];
        }
        tmp[i]=1;
        ans.push(tmp);
    }
    return ans[index];
};
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

Top comments (1)

Collapse
 
coderduck profile image
duccanhole

thanks you !