leetcode38——Count and Say

原创 2018年04月17日 15:06:18

题目大意:数数并记录,求出第n个字符串序列,字符串序列规律如下:

                第一个:1(初始序列)

                第二个:11(11:在描述第一个序列)

                第三个:21(21:在描述第二个序列)

                第四个:1211(12+11:在描述第三个序列

                第五个:111221(11+12+21:在描述第四个序列)

分析:循环n-1次遍历上一个字符串得到当前答案字符串。

代码:遍历n-1次字符串

class Solution {
public:
string countAndSay(int n) {
string tmp = "1";
for (int i = 1;i < n;i++) {  //第n个序列就是循环n-1次这个过程
int j = 0;
int k = 1;  //记录当前字符已有个数
string now = "";
while (j < tmp.size()) {
if (tmp[j] == tmp[j + 1]) {
k++;
}
else {
char count = k + '0';
now = now + count + tmp[j];
k = 1;
}
j++;
}
tmp = now;
}
return tmp;
}

};

LeetCode(38) Count and Say

题目The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111...
  • fly_yr
  • fly_yr
  • 2015-08-26 11:14:38
  • 3836

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say  The count-and-say sequence is the sequence of integers beginning as follows: 1, 1...
  • xygy8860
  • xygy8860
  • 2015-07-09 20:40:16
  • 4141

LeetCode --- 38. Count and Say

题目链接:Count and Say The count-and-say sequence is the sequence of integers beginning as follows: ...
  • makuiyu
  • makuiyu
  • 2015-02-05 23:33:41
  • 2923

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...
  • runningtortoises
  • runningtortoises
  • 2015-05-19 21:36:24
  • 2427

LeetCode38——Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11122...
  • booirror
  • booirror
  • 2015-02-11 18:43:12
  • 1272

Leetcode38 Count and Say

Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 1...
  • u011513853
  • u011513853
  • 2015-07-29 15:37:40
  • 221

Leetcode38 - Count And Say

Leetcode38 - count and say
  • ShelaGao
  • ShelaGao
  • 2017-09-18 17:58:25
  • 240

leetcode38 Count and Say

package leetcode38; import java.util.ArrayList; /** * The count-and-say sequence is the sequence ...
  • fanyanqin65
  • fanyanqin65
  • 2015-10-21 16:24:51
  • 178

[LeetCode38]Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...
  • sbitswc
  • sbitswc
  • 2014-06-17 03:40:09
  • 581
收藏助手
不良信息举报
您举报文章:leetcode38——Count and Say
举报原因:
原因补充:

(最多只允许输入30个字)