# 算法题-Count and Say

+关注继续查看

The count-and-say sequence is the sequence of integers with the first five terms as following:

1. 1
2. 11
3. 21
4. 1211
5. 111221

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.
Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: “1”
Example 2:

Input: 4
Output: “1211”

class Solution {
public String countAndSay(int n) {
if (n == 1){
return "1";
}
String str = countAndSay(n - 1);
StringBuilder result = new StringBuilder();
int count = 0;
int numberIndex = 0;
int i = 0;
while (i < str.length()){
if (str.charAt(numberIndex) == str.charAt(i)){
count++;
if (i + 1 == str.length()){
result.append(String.valueOf(count)).append(String.valueOf(str.charAt(numberIndex)));
}
}else {
result.append(String.valueOf(count)).append(String.valueOf(str.charAt(numberIndex)));
numberIndex = i;
count = 0;
continue;
}
i++;
}
return result.toString();
}
}

ML之NB：利用NB朴素贝叶斯算法(CountVectorizer/TfidfVectorizer+去除停用词)进行分类预测、评估
ML之NB：利用NB朴素贝叶斯算法(CountVectorizer/TfidfVectorizer+去除停用词)进行分类预测、评估
52 0

20690 0
C++ STL算法系列1---count函数

621 0

18989 0

25226 0

1216 0
ML之NB：利用朴素贝叶斯NB算法(CountVectorizer+不去除停用词)对fetch_20newsgroups数据集(20类新闻文本)进行分类预测、评估
ML之NB：利用朴素贝叶斯NB算法(CountVectorizer+不去除停用词)对fetch_20newsgroups数据集(20类新闻文本)进行分类预测、评估
74 0
+关注
D_H_T
java，编程语言相关技术专家
42

0

JS零基础入门教程（上册）