返回介绍

Count and Say

发布于 2025-02-22 13:01:22 字数 2636 浏览 0 评论 0 收藏 0

Source

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.

Example
Given n = 5, return "111221".

Note
The sequence of integers will be represented as a string.

题解

题目大意是找第 n 个数(字符串表示),规则则是对于连续字符串,表示为重复次数+数本身。

C++

string countAndSay(int n) {
  if (n == 0) return "";
  string res = "1";
  while (--n) {
    string cur = "";
    for (int i = 0; i < res.size(); i++) {
      int count = 1;
       while ((i + 1 < res.size()) && (res[i] == res[i + 1])){
        count++;   
        i++;
      }
      cur += to_string(count) + res[i];
    }
    res = cur;
  }
  return res;
}

Java

public class Solution {
  /**
   * @param n the nth
   * @return the nth sequence
   */
  public String countAndSay(int n) {
    if (n <= 0) return null;

    String s = "1";
    for (int i = 1; i < n; i++) {
      int count = 1;
      StringBuilder sb = new StringBuilder();
      int sLen = s.length();
      for (int j = 0; j < sLen; j++) {
        if (j < sLen - 1 && s.charAt(j) == s.charAt(j + 1)) {
          count++;
        } else {
          sb.append(count + "" + s.charAt(j));
          // reset
          count = 1;
        }
      }
      s = sb.toString();
    }

    return s;
  }
}

源码分析

字符串是动态生成的,故使用 StringBuilder 更为合适。注意 s 初始化为"1", 第一重 for 循环中注意循环的次数为 n-1.

复杂度分析

题解 2 - 递归

C++

class Solution {
public:
  string countAndSay(int n) {
    if (n == 1) return "1";       // base case
    string res, tmp = countAndSay(n - 1);  // recursion
    char c = tmp[0];
    int count = 1;
    for (int i = 1; i < tmp.size(); i++)
      if (tmp[i] == c)
        count++;
      else {
        res += to_string(count);
        res.push_back(c);
        c = tmp[i];
        count = 1;
      }
    res += to_string(count);
    res.push_back(c);
    return res;
  }
};

Reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文