๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/leetcode

LeetCode 118 - Pascal's Triangle (Easy)

by HandHand 2021. 3. 3.

๋ฌธ์ œ

LeetCode - 118๋ฒˆ

ํ’€์ด ๊ณผ์ •

ํŒŒ์Šค์นผ ์‚ผ๊ฐํ˜•์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

1๋ฒˆ์งธ ํ–‰๊ณผ 2๋ฒˆ์งธ ํ–‰์€ ๊ฐ๊ฐ [1] ๊ณผ [1, 1] ๋กœ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ 
๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ๋Š” ๋งˆ์ง€๋ง‰ ํ–‰์˜ ์ธ์ ‘ํ•œ ์š”์†Œ๋“ค์„ ๋”ํ•ด์„œ ์ƒˆ๋กœ์šด ํ–‰์„ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ

/**
 * @param {number} numRows
 * @return {number[][]}
 */
var generate = function (numRows) {
  const answer = [];
  let walker = 1;

  while (walker <= numRows) {
    if (walker === 1) answer.push([1]);
    else if (walker === 2) answer.push([1, 1]);
    else {
      const lastRow = answer[answer.length - 1];
      const temp = [];
      for (let i = 0; i < lastRow.length - 1; i++) {
        temp.push(lastRow[i] + lastRow[i + 1]);
      }

      answer.push([1, ...temp, 1]);
    }

    walker++;
  }

  return answer;
};
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

LeetCode 3 - Longest Substring Without Repeating Characters (Medium)  (0) 2021.03.03
LeetCode 89 - Gray Code (Medium)  (0) 2021.03.03
LeetCode 19 - Remove Nth Node From End of List (Medium)  (0) 2021.03.03
LeetCode 79 - Word Search (Medium)  (0) 2021.03.03
LeetCode 771 - Jewels and Stones (Easy)  (0) 2021.03.03

๐Ÿ’ฌ ๋Œ“๊ธ€