Skip to content

Commit 7f1a927

Browse files
committed
38 count-and-say
1 parent fcf983c commit 7f1a927

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} n
3+
* @return {string}
4+
*/
5+
var countAndSay = function (n) {
6+
if (n == 1) return "1";
7+
if (n == 2) return "11";
8+
9+
let res = "11";
10+
for (let i = 3; i <= n; i++) {
11+
res += '$';
12+
let len = res.length;
13+
let counter = 1;
14+
let tempStr = "";
15+
let arr = res.split('');
16+
17+
for (let j = 1; j < len; j++) {
18+
if (arr[j] !== arr[j - 1]) {
19+
tempStr += counter + 0;
20+
tempStr += arr[j - 1];
21+
counter = 1;
22+
23+
} else
24+
counter++;
25+
}
26+
res = tempStr;
27+
}
28+
return res;
29+
};
30+
31+
console.log(countAndSay(10))

easy/0038 count-and-say/readme.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Count and Say
2+
3+
The count-and-say sequence is the sequence of integers with the first five terms as following:
4+
5+
1 ====> "1"
6+
7+
2 ====> "11"
8+
9+
3 ====> "21"
10+
11+
4 ====> "1211"
12+
13+
5 ====> "111221"
14+
15+
1 is read off as "one 1" or 11.
16+
17+
11 is read off as "two 1s" or 21.
18+
19+
21 is read off as "one 2, then one 1" or 1211.
20+
21+
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
22+
23+
Note: Each term of the sequence of integers will be represented as a string.
24+
25+
## Example 1
26+
27+
Input: 1
28+
29+
Output: "1"
30+
31+
## Example 2
32+
33+
Input: 4
34+
35+
Output: "1211"
36+
37+
## More Info
38+
39+
<https://leetcode.com/problems/count-and-say/>

medium/0036 valid-sudoku/readme.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Valid Sudoku
2+
3+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
4+
5+
Each row must contain the digits 1-9 without repetition.
6+
7+
Each column must contain the digits 1-9 without repetition.
8+
9+
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
10+
11+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
12+
13+
## Example 1
14+
15+
Input:
16+
17+
[
18+
19+
["5","3",".",".","7",".",".",".","."],
20+
21+
["6",".",".","1","9","5",".",".","."],
22+
23+
[".","9","8",".",".",".",".","6","."],
24+
25+
["8",".",".",".","6",".",".",".","3"],
26+
27+
["4",".",".","8",".","3",".",".","1"],
28+
29+
["7",".",".",".","2",".",".",".","6"],
30+
31+
[".","6",".",".",".",".","2","8","."],
32+
33+
[".",".",".","4","1","9",".",".","5"],
34+
35+
[".",".",".",".","8",".",".","7","9"]
36+
37+
]
38+
39+
Output: true
40+
41+
## Example 2
42+
43+
Input:
44+
[
45+
["8","3",".",".","7",".",".",".","."],
46+
["6",".",".","1","9","5",".",".","."],
47+
[".","9","8",".",".",".",".","6","."],
48+
["8",".",".",".","6",".",".",".","3"],
49+
["4",".",".","8",".","3",".",".","1"],
50+
["7",".",".",".","2",".",".",".","6"],
51+
[".","6",".",".",".",".","2","8","."],
52+
[".",".",".","4","1","9",".",".","5"],
53+
[".",".",".",".","8",".",".","7","9"]
54+
]
55+
56+
Output: false
57+
58+
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

medium/0036 valid-sudoku/valid-sudoku.js

Whitespace-only changes.

0 commit comments

Comments
 (0)