zoukankan      html  css  js  c++  java
  • 力扣算法题—038报数

     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 }
  • 相关阅读:
    SpringMVC扩展
    反射机制
    python day9
    python day8
    python day7
    python day6
    python day4
    python day3
    python day2
    python day1
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10559014.html
Copyright © 2011-2022 走看看