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

LeetCode 38 - Count and Say (Medium)

by HandHand 2021. 4. 13.

๋ฌธ์ œ

LeetCode - 38๋ฒˆ

ํ’€์ด ๊ณผ์ •

์ฃผ์–ด์ง„ ๊ทœ์น™๋Œ€๋กœ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์—ฐ์†๋œ ์ˆซ์ž๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ

/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function (n) {
  let str = "1";

  for (let i = 1; i < n; i += 1) {
    let cacheKey = "";
    let count = 0;
    let temp = "";

    for (let ch of str) {
      if (cacheKey !== ch) {
        if (cacheKey) {
          temp += `${count}${cacheKey}`;
        }

        cacheKey = ch;
        count = 1;
      } else {
        count += 1;
      }
    }

    temp += `${count}${cacheKey}`;
    str = temp;
  }

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

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

LeetCode 36 - Valid Sudoku (Medium)  (0) 2021.04.13
LeetCode 120 - Triangle (Medium)  (0) 2021.04.13
LeetCode 199 - Binary Tree Right Side View (Medium)  (0) 2021.04.05
LeetCode 344 - Reverse String (Easy)  (0) 2021.04.05
LeetCode 1578 - Minimum Deletion Cost to Avoid Repeating Letters (Medium)  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€