zoukankan      html  css  js  c++  java
  • LeetCode-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.

     class Solution {
     public:
         string countAndSay(int n) {
             // Start typing your C/C++ solution below
             // DO NOT write int main() function
             vector<string> a;
             stringstream ss2;
             a.push_back("1");
             string s;
             string o,o1;
             for(int i=1;i<n;i++){
                 s=a[i-1];
                 char current=s[0];
                 int count=1;
                 o.clear();
                 for(int j=1;j<s.length();j++){
                     if(s[j]!=current){
                         ss2.clear();
                         ss2<<count;
                         o1.clear();
                         ss2>>o1;
                         o+=o1;
                         o+=current;
                         current=s[j];
                         count=1;
                     }
                     else{
                         count++;
                     }
                 }
                 ss2.clear();
                 ss2<<count;
                 o1.clear();
                 ss2>>o1;
                 o+=o1;
                 o+=current;
                 a.resize(a.size()+1);
                 a[a.size()-1]=o;
             }
             return a[a.size()-1];
         }
     };
  • 相关阅读:
    推导式
    解构
    for 循环
    运算符
    while 循环
    流程控制语句
    索引和切片
    ASC转换BCD,ASC2BCD(转)
    CString和char互转,十六进制的BYTE转CString
    C++添加简单的日记记录
  • 原文地址:https://www.cnblogs.com/superzrx/p/3334051.html
Copyright © 2011-2022 走看看