当前位置:   article > 正文

C++ | Leetcode C++题解之第38题外观数列

C++ | Leetcode C++题解之第38题外观数列

题目:

题解:

  1. class Solution {
  2. public:
  3. string countAndSay(int n) {
  4. string prev = "1";
  5. for (int i = 2; i <= n; ++i) {
  6. string curr = "";
  7. int start = 0;
  8. int pos = 0;
  9. while (pos < prev.size()) {
  10. while (pos < prev.size() && prev[pos] == prev[start]) {
  11. pos++;
  12. }
  13. curr += to_string(pos - start) + prev[start];
  14. start = pos;
  15. }
  16. prev = curr;
  17. }
  18. return prev;
  19. }
  20. };
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/457255
推荐阅读
相关标签
  

闽ICP备14008679号