首页 > 编程语言 > 详细

力扣算法题—038报数

时间:2019-03-19 16:16:47      阅读:301      评论:0      收藏:0      [点我收藏+]
 1 #include "000库函数.h"
 2 
 3 
 4 //自解,就遍历数数  8ms
 5 class Solution {
 6 public:
 7     string countAndSay(int n) {
 8         if (n == 0)return "";
 9         string str = "1";
10         string s;
11         for (int i = 1; i < n; ++i) {
12             s = "";
13             int n = 0;
14             char a = str[0];
15             for (int j = 0; j < str.size(); ++j) {
16                 if (str[j] == a)
17                     ++n;
18                 else {
19                     s += n + 0;
20                     s += a;
21                     a = str[j];
22                     n = 1;
23                 }
24             }
25             s += n + 0;
26             s += a;
27             str = s;
28         }
29         return str;
30     }
31 };
32 
33 void T038() {
34     Solution s;
35     string str;
36     str = s.countAndSay(4);
37     cout << str << endl;
38     str = s.countAndSay(1);
39     cout << str << endl;
40     str = s.countAndSay(5);
41     cout << str << endl;
42 }

 

力扣算法题—038报数

原文:https://www.cnblogs.com/zzw1024/p/10559014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!