Skip to content

Commit 2a11645

Browse files
committed
add 038
1 parent 17fc860 commit 2a11645

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
| 125 | [Valid Palindrome][125] |
3939
| 8 | [String to Integer (atoi)][008] |
4040
| 28 | [Implement strStr()][028] |
41+
| 38 | [count-and-say][038] |
4142

4243

4344
**其他**
@@ -71,3 +72,4 @@
7172
[125]: https://github.com/andavid/leetcode-java/blob/master/note/125/README.md
7273
[008]: https://github.com/andavid/leetcode-java/blob/master/note/008/README.md
7374
[028]: https://github.com/andavid/leetcode-java/blob/master/note/028/README.md
75+
[038]: https://github.com/andavid/leetcode-java/blob/master/note/038/README.md

note/038/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [count-and-say][title]
2+
3+
## Description
4+
5+
The count-and-say sequence is the sequence of integers with the first five terms as following:
6+
7+
```
8+
1. 1
9+
2. 11
10+
3. 21
11+
4. 1211
12+
5. 111221
13+
```
14+
15+
1 is read off as "one 1" or 11.
16+
11 is read off as "two 1s" or 21.
17+
21 is read off as "one 2, then one 1" or 1211.
18+
Given an integer n, generate the nth term of the count-and-say sequence.
19+
20+
Note: Each term of the sequence of integers will be represented as a string.
21+
22+
**Example 1:**
23+
24+
```
25+
Input: 1
26+
Output: "1"
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: 4
33+
Output: "1211"
34+
```
35+
36+
## 思路
37+
遍历字符串,如果当前字符和上一个字符不相同,则输出计数情况,否则计数器加1。
38+
39+
## [完整代码][src]
40+
41+
```java
42+
class Solution {
43+
public String countAndSay(int n) {
44+
String str = "1";
45+
for (int i = 1; i < n; i++) {
46+
str = say(str);
47+
}
48+
return str;
49+
}
50+
51+
public String say(String str) {
52+
if (str.length() == 1) {
53+
return "1" + str;
54+
}
55+
StringBuilder sb = new StringBuilder();
56+
int count = 1;
57+
int i = 1;
58+
for (; i < str.length(); i++) {
59+
if (str.charAt(i) != str.charAt(i - 1)) {
60+
sb.append(count).append(str.charAt(i - 1));
61+
count = 1;
62+
} else {
63+
count++;
64+
}
65+
}
66+
sb.append(count).append(str.charAt(i - 1));
67+
return sb.toString();
68+
}
69+
}
70+
```
71+
72+
[title]: https://leetcode.com/problems/count-and-say
73+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_038/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public String countAndSay(int n) {
3+
String str = "1";
4+
for (int i = 1; i < n; i++) {
5+
str = say(str);
6+
}
7+
return str;
8+
}
9+
10+
public String say(String str) {
11+
if (str.length() == 1) {
12+
return "1" + str;
13+
}
14+
StringBuilder sb = new StringBuilder();
15+
int count = 1;
16+
int i = 1;
17+
for (; i < str.length(); i++) {
18+
if (str.charAt(i) != str.charAt(i - 1)) {
19+
sb.append(count).append(str.charAt(i - 1));
20+
count = 1;
21+
} else {
22+
count++;
23+
}
24+
}
25+
sb.append(count).append(str.charAt(i - 1));
26+
return sb.toString();
27+
}
28+
29+
public static void main(String[] args) {
30+
Solution solution = new Solution();
31+
System.out.println(solution.countAndSay(1));
32+
System.out.println(solution.countAndSay(8));
33+
}
34+
}

0 commit comments

Comments
 (0)