c_cpp 38.算和说
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 38.算和说相关的知识,希望对你有一定的参考价值。
//Runtime: 8 ms, faster than 75.88%
//Memory Usage: 8.7 MB, less than 83.18%
class Solution {
public:
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;
}
};
//Runtime: 4 ms, faster than 100.00%
//Memory Usage: 8.6 MB, less than 95.80%
class Solution {
public:
string countAndSay(int n) {
string res = "1",cur = "1";
while(--n){
char ch = res[0];
int count = 0;
cur = "";
for(int i = 0;i < res.size();i++){
if(ch == res[i])
count++;
else{
cur += count + '0';
cur += ch;
ch = res[i];
count = 1;
}
}
cur += count + '0';
cur += ch;
res = cur;
}
return res;
}
};
以上是关于c_cpp 38.算和说的主要内容,如果未能解决你的问题,请参考以下文章
java 数数和说
为啥看和说序列的这两种实现有不同的执行时间?
在 C++ 中使用递归计算和说
c_cpp 38.cpp
[Leetcode] count and say 计数和说
ESP32上手笔记 | 05 - 获取MPU6050数据进行姿态解算和展示(I2Cdev+MPU6050+Processing)