Skip to content

Commit 98005ee

Browse files
committed
38. Count and Say
1 parent 0416fb6 commit 98005ee

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/leetcode/_38_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._38_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.countAndSay(1));
10+
System.out.println(solution.countAndSay(2));
11+
System.out.println(solution.countAndSay(3));
12+
System.out.println(solution.countAndSay(4));
13+
}
14+
}
15+

src/leetcode/_38_/Solution.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode._38_;
2+
3+
class Solution {
4+
public String countAndSay(int n) {
5+
String tmp = "1";
6+
if (n == 1) {
7+
return tmp;
8+
}
9+
for (int i = 1; i < n; i++) {
10+
tmp = say(tmp);
11+
}
12+
return tmp;
13+
}
14+
15+
private String say(String numStr) {
16+
char tmp = numStr.charAt(0);
17+
int counter = 1;
18+
StringBuilder sb = new StringBuilder();
19+
for (int i = 1; i < numStr.length(); i++) {
20+
if (numStr.charAt(i) == numStr.charAt(i - 1)) {
21+
counter++;
22+
} else {
23+
sb.append(counter).append(tmp);
24+
tmp = numStr.charAt(i);
25+
counter = 1;
26+
}
27+
}
28+
sb.append(counter).append(tmp);
29+
return sb.toString();
30+
}
31+
}

src/leetcode/_38_/solution.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
### [38\. Count and SayCopy for MarkdownCopy for Markdown](https://leetcode.com/problems/count-and-say/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
The count-and-say sequence is the sequence of integers with the first five terms as following:
7+
8+
```
9+
1\. 1
10+
2\. 11
11+
3\. 21
12+
4\. 1211
13+
5\. 111221
14+
```
15+
16+
`1` is read off as `"one 1"` or `11`.
17+
`11` is read off as `"two 1s"` or `21`.
18+
`21` is read off as `"one 2`, then `one 1"` or `1211`.
19+
20+
Given an integer _n_ where 1 ≤ _n_ ≤ 30, generate the _n_<sup>th</sup> term of the count-and-say sequence.
21+
22+
Note: Each term of the sequence of integers will be represented as a string.
23+
24+
**Example 1:**
25+
26+
```
27+
Input: 1
28+
Output: "1"
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: 4
35+
Output: "1211"```
36+
37+
38+
#### Solution
39+
40+
Language: **Java**
41+
42+
```java
43+
class Solution {
44+
   public String countAndSay(int n) {
45+
       String tmp = "1";
46+
       if (n == 1) {
47+
           return tmp;
48+
      }
49+
       for (int i = 1; i < n; i++) {
50+
           tmp = say(tmp);
51+
      }
52+
       return tmp;
53+
  }
54+
55+
   private String say(String numStr) {
56+
       char tmp = numStr.charAt(0);
57+
       int counter = 1;
58+
       StringBuilder sb = new StringBuilder();
59+
       for (int i = 1; i < numStr.length(); i++) {
60+
           if (numStr.charAt(i) == numStr.charAt(i - 1)) {
61+
               counter++;
62+
          } else {
63+
               sb.append(counter).append(tmp);
64+
               tmp = numStr.charAt(i);
65+
               counter = 1;
66+
          }
67+
      }
68+
       sb.append(counter).append(tmp);
69+
       return sb.toString();
70+
  }
71+
}
72+
```
73+
![](https://ws4.sinaimg.cn/large/006tKfTcgy1g1ao5345dcj311e0niq6k.jpg)

0 commit comments

Comments
 (0)