๋ฌธ์
ํ์ด ๊ณผ์
์ฃผ์ด์ง ๊ท์น๋๋ก ๋ฌธ์์ด ์ฒ๋ฆฌ๋ฅผ ๊ตฌํํ๋ ๋ฌธ์ ์
๋๋ค.
์ฐ์๋ ์ซ์๋ค์ ๊ฐ์๋ฅผ ์ธ์ด์ฃผ๊ธฐ ์ํด ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
์ฝ๋
/**
* @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 |
๐ฌ ๋๊ธ