Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- i/p = 5;
- o/p = nested array of length 5
- cons- none
- edge cases- none
- just- create arrays that increases in length as it iterates
- from 0 to n. For each iteration add up the prev elements to
- create new element
- visulization:
- as above
- pseudocode:
- // create a place holder array = main arr
- //place first elem = [1] in main arr
- // use for loop for numrows iterations
- //for iteration 2,
- //crete temp arr = [];
- // push temparr [1,1]
- //arr [0] = 1,
- //arr [1] = temparr[0]+[1];
- //arr.length-1 = [1;]
- transformation steps
- 1 | [1,1] | [1,1]
- 2 | [1,1] | [1,2,1]
- 3 | [1,2,1] | [1,3,3,1]
- 4 | [1,3,3,1] | [1,4,6,4,1]
- */
- var generate = function(numRows) {
- var results = [];
- var firstArr = [];
- firstArr[0] = 1;
- results.push(firstArr);
- var count = 0;
- var tempArr;
- for (var i = 0; i < numRows; i++) {
- tempArr = [1];
- for (var j = 1; j < results[i].length; j++) {
- tempArr.push(results[i][j] + results[i][j-1])
- }
- tempArr.push(1);
- results.push(tempArr);
- }
- return results;
- };
- // for (var i = 2; i <= numRows; i++) {
- // var tempArr = [];
- // tempArr[0] = 1;
- // tempArr[i-1] = 1;
- // if(i === 2) {
- // results.push(tempArr);
- // }
- // var x = i-2;
- // var holder = [];
- // for (var j=3; j < numRows; j++) {
- // holder[x] = tempArr[j-2] + tempArr[j-1];
- // holder.concat(tempArr)
- // }
- // }
- // results.push(tempArr);
- // }
Add Comment
Please, Sign In to add comment