Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 1.24 KB

38报数.md

File metadata and controls

58 lines (47 loc) · 1.24 KB

题目描述

https://leetcode-cn.com/problems/count-and-say/
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221

思路一

字符串模拟,一步一步照着题意来呗

class Solution {
public:
    string countAndSay(int n) {
        string res = "1";
        
        n--;
            
        while(n--)
        {
            int len = res.size();
            string temp = "";
            
            int begin = 0;
            int end = 0;

            while(1)
            {
                if( end == len-1 ){
                    temp += ('0' + end-begin+1);
                    temp += res[begin];

                    break;
                }
                else if( res[end+1] == res[begin] ){
                    end++;
                }
                else{
                    temp += ('0' + end-begin+1);
                    temp += res[begin];
                    
                    begin = end+1;
                    end = begin;
                }
            }
            
            res = temp;
            //cout << res << endl;
        }
        
        return res;
    }
};