Maple's Blog.

Leetcode-38

字数统计: 228阅读时长: 1 min
2019/04/10 Share

countAndSay-链接

实现的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
string countAndSay(int n) {
vector<string>s(n+1,"");
s[1]="1";
for(int i=2;i<=n;i++)
{
for(int j=0;s[i-1][j]!='\0';j++)
{
int x=j;
int count=1;
while(s[i-1][x]==s[i-1][j+1])
{
j++;
count++;
}
int f='0'+count;
s[i]+=f;
s[i]+=s[i-1][x];
}
}
return s[n];

}
};

刚刚开始看的时候题目意思都不知道…….后来发现它这个题目的意思其实是这样的,比如说现在知道1是”1“,那么2就是在已知1答案的情况下将1的答案报出来,就是1个1,也就是”11“;然后3的答案就是在已知2的答案的情况下将2的答案报出来,就是2个1,就是”21“;然后4的答案就是报3的答案,1个2,1个1,就是”1211“…….

这个题有点逗比……

CATALOG