Skip to content

Commit ba6178d

Browse files
committed
feat: ADD 038
1 parent d1c738c commit ba6178d

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88
|:------------- |:------------- |:------------- |
99
|1|[Two Sum][001]|Array, Hash Table|
1010
|7|[Reverse Integer][007]|Math|
11-
|8|[String to Integer (atoi)][008]|Math, String|
1211
|9|[Palindrome Number][009]|Math|
1312
|13|[Roman to Integer][013]|Math, String|
1413
|14|[Longest Common Prefix][014]|String|
15-
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
1614
|20|[Valid Parentheses][020]|Stack, String|
1715
|21|[Merge Two Sorted Lists][021]|Linked List|
1816
|26|[Remove Duplicates from Sorted Array][026]|Array, Two Pointers|
1917
|27|[Remove Element][027]|Array, Two Pointers|
2018
|28|[Implement strStr()][028]|Two Pointers, String|
19+
|38|[Count and Say][028]|String|
2120

2221

2322
## Medium
2423

2524
|#|Title|Tag|
2625
|:------------- |:------------- |:------------- |
2726
|2|Add Two Numbers|Linked List, Math|
27+
|8|[String to Integer (atoi)][008]|Math, String|
28+
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
2829

2930

3031
## Hard
@@ -50,3 +51,4 @@
5051
[026]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/026/README.md
5152
[027]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/027/README.md
5253
[028]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/028/README.md
54+
[038]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/038/README.md

note/038/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [Count and Say][title]
2+
3+
## Description
4+
5+
The count-and-say sequence is the sequence of integers beginning as follows:
6+
7+
`1, 11, 21, 1211, 111221, ...`
8+
9+
`1` is read off as `"one 1"` or `11`.
10+
11+
`11` is read off as `"two 1s"` or `21`.
12+
13+
`21` is read off as `"one 2`, then `one 1"` or `1211`.
14+
15+
Given an integer *n*, generate the *n*<sup>th</sup> sequence.
16+
17+
Note: The sequence of integers will be represented as a string.
18+
19+
**Tags:** String
20+
21+
22+
## 思路
23+
24+
题意是数和说,根据如下序列`1, 11, 21, 1211, 111221, ...`,求第n个数,规则很简单,就是数和说,数就是数这个数数字有几个,说就是说这个数,所以`1`就是1个1:`11`,`11`就是有2个1:`21``21`就是1个2、1个1:`1211`,可想而知后面就是`111221`,思路的话就是按这个逻辑模拟出来即可。
25+
26+
``` java
27+
public class Solution {
28+
public String countAndSay(int n) {
29+
String str = "1";
30+
while (--n > 0) {
31+
int times = 1;
32+
StringBuilder sb = new StringBuilder();
33+
char[] chars = str.toCharArray();
34+
int len = chars.length;
35+
for (int j = 1; j < len; j++) {
36+
if (chars[j - 1] == chars[j]) {
37+
times++;
38+
} else {
39+
sb.append(times).append(chars[j - 1]);
40+
times = 1;
41+
}
42+
}
43+
str = sb.append(times).append(chars[len - 1]).toString();
44+
}
45+
return str;
46+
}
47+
}
48+
```
49+
50+
51+
## 结语
52+
53+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
54+
55+
56+
57+
[title]: https://leetcode.com/problems/count-and-say
58+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.blankj.easy._038;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/21
8+
* desc :
9+
* </pre>
10+
*/
11+
12+
public class Solution {
13+
public String countAndSay(int n) {
14+
String str = "1";
15+
while (--n > 0) {
16+
int times = 1;
17+
StringBuilder sb = new StringBuilder();
18+
char[] chars = str.toCharArray();
19+
int len = chars.length;
20+
for (int j = 1; j < len; j++) {
21+
if (chars[j - 1] == chars[j]) {
22+
times++;
23+
} else {
24+
sb.append(times).append(chars[j - 1]);
25+
times = 1;
26+
}
27+
}
28+
str = sb.append(times).append(chars[len - 1]).toString();
29+
}
30+
return str;
31+
}
32+
33+
public static void main(String[] args) {
34+
Solution solution = new Solution();
35+
for (int i = 1; i < 6; i++) {
36+
System.out.println(solution.countAndSay(i));
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)