DEV Community

duccanhole
duccanhole

Posted on

code every day with me

--DAY 14--

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
  • Detail: here
  • My solution (javascript):
var generate = function(n) {
    if(n==1) return [[1]];
    if(n==2) return [[1],[1,1]];
    let arr=[[1],[1,1]];
    for(let i=2;i<n;i++){
        arr[i]=[1];
        for(let j=1;j<i;j++){
            arr[i].push(arr[i-1][j-1]+arr[i-1][j]);
        }
        arr[i].push(1);
    }
    return arr;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)