Skip to content

Latest commit

 

History

History

count-and-say

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Count and say

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/count-and-say

class Solution {
 public:
  string countAndSay(int n, const string& s = "1") {
    if (n == 1) return s;
    string ret;
    for (int i = 0, cur = 0;; ++i) {
      if (!s[i]) {
        ret += to_string(cur) + s[i - 1];
        break;
      }
      if (!i or s[i] == s[i - 1])
        ++cur;
      else {
        ret += to_string(cur) + s[i - 1];
        cur = 1;
      }
    }
    return countAndSay(n - 1, ret);
  }
};

Tags