• Count and Say


    The count-and-say sequence is the sequence of integers beginning as follows:
    1, 11, 21, 1211, 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 sequence.
    Note: The sequence of integers will be represented as a string.

     1 class Solution {
     2 public:
     3     string countAndSay(int n) {
     4         string res = "1";
     5         for(int i = 2; i <= n; i++) {
     6             stringstream ss;
     7             int k = 1;
     8             for(int j = 1; j < res.size(); j++) {
     9                 if(res[j] == res[j-1]) k++;
    10                 else if(res[j] != res[j-1]) {
    11                     ss << k << res[j-1];
    12                     k = 1;
    13                 }
    14             }
    15             ss << k << res[res.size()-1];
    16             ss >> res;
    17         }
    18         return res;
    19     }
    20 };
  • 相关阅读:
    实验报告2
    实验三 网络欺骗技术
    实验3
    心理魔术
    实验报告
    实验4
    实验5
    实验四恶意代码
    网络对抗技术 实验一
    实验二
  • 原文地址:https://www.cnblogs.com/zhengjiankang/p/3646871.html
Copyright © 2020-2023  润新知