Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d861c7

Browse files
String: 38. Count and Say
1 parent 1b207ed commit 9d861c7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎String/38. Count and Say.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/count-and-say/description/
2+
/**
3+
* 38. Count and Say
4+
* @param {number} n
5+
* @return {string}
6+
*/
7+
const arr = [,'1']
8+
var countAndSay = function(n) {
9+
if (n === 1) return '1';
10+
if (arr[n] !== undefined) return arr[n];
11+
let str = countAndSay(n - 1);
12+
let i = 1;
13+
let last = str[0];
14+
let count = 1;
15+
let res = '';
16+
while (i < str.length) {
17+
if (str[i] === last) {
18+
count++;
19+
} else {
20+
res += count + last;
21+
last = str[i];
22+
count = 1;
23+
}
24+
i++;
25+
}
26+
res += count + last;
27+
return arr[n] = res;
28+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /