comments | difficulty | edit_url | tags | |
---|---|---|---|---|
true |
中等 |
|
「外观数列」是一个数位字符串序列,由递归公式定义:
countAndSay(1) = "1"
countAndSay(n)
是countAndSay(n-1)
的行程长度编码。
行程长度编码(RLE)是一种字符串压缩方法,其工作原理是通过将连续相同字符(重复两次或更多次)替换为字符重复次数(运行长度)和字符的串联。例如,要压缩字符串 "3322251"
,我们将 "33"
用 "23"
替换,将 "222"
用 "32"
替换,将 "5"
用 "15"
替换并将 "1"
用 "11"
替换。因此压缩后字符串变为 "23321511"
。
给定一个整数 n
,返回 外观数列 的第 n
个元素。
示例 1:
输入:n = 4
输出:"1211"
解释:
countAndSay(1) = "1"
countAndSay(2) = "1" 的行程长度编码 = "11"
countAndSay(3) = "11" 的行程长度编码 = "21"
countAndSay(4) = "21" 的行程长度编码 = "1211"
示例 2:
输入:n = 1
输出:"1"
解释:
这是基本情况。
提示:
1 <= n <= 30
进阶:你能迭代解决该问题吗?
class Solution:
def countAndSay(self, n: int) -> str:
s = '1'
for _ in range(n - 1):
i = 0
t = []
while i < len(s):
j = i
while j < len(s) and s[j] == s[i]:
j += 1
t.append(str(j - i))
t.append(str(s[i]))
i = j
s = ''.join(t)
return s
class Solution {
public String countAndSay(int n) {
String s = "1";
while (--n > 0) {
StringBuilder t = new StringBuilder();
for (int i = 0; i < s.length();) {
int j = i;
while (j < s.length() && s.charAt(j) == s.charAt(i)) {
++j;
}
t.append((j - i) + "");
t.append(s.charAt(i));
i = j;
}
s = t.toString();
}
return s;
}
}
class Solution {
public:
string countAndSay(int n) {
string s = "1";
while (--n) {
string t = "";
for (int i = 0; i < s.size();) {
int j = i;
while (j < s.size() && s[j] == s[i]) ++j;
t += to_string(j - i);
t += s[i];
i = j;
}
s = t;
}
return s;
}
};
func countAndSay(n int) string {
s := "1"
for k := 0; k < n-1; k++ {
t := &strings.Builder{}
i := 0
for i < len(s) {
j := i
for j < len(s) && s[j] == s[i] {
j++
}
t.WriteString(strconv.Itoa(j - i))
t.WriteByte(s[i])
i = j
}
s = t.String()
}
return s
}
function countAndSay(n: number): string {
let s = '1';
for (let i = 1; i < n; i++) {
let t = '';
let cur = s[0];
let count = 1;
for (let j = 1; j < s.length; j++) {
if (s[j] !== cur) {
t += `${count}${cur}`;
cur = s[j];
count = 0;
}
count++;
}
t += `${count}${cur}`;
s = t;
}
return s;
}
use std::iter::once;
impl Solution {
pub fn count_and_say(n: i32) -> String {
(1..n)
.fold(vec![1], |curr, _| {
let mut next = vec![];
let mut slow = 0;
for fast in 0..=curr.len() {
if fast == curr.len() || curr[slow] != curr[fast] {
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
slow = fast;
}
}
next
})
.into_iter()
.map(|digit| (digit + b'0') as char)
.collect()
}
}
const countAndSay = function (n) {
let s = '1';
for (let i = 2; i <= n; i++) {
let count = 1,
str = '',
len = s.length;
for (let j = 0; j < len; j++) {
if (j < len - 1 && s[j] === s[j + 1]) {
count++;
} else {
str += `${count}${s[j]}`;
count = 1;
}
}
s = str;
}
return s;
};
using System.Text;
public class Solution {
public string CountAndSay(int n) {
var s = "1";
while (n > 1)
{
var sb = new StringBuilder();
var lastChar = '1';
var count = 0;
foreach (var ch in s)
{
if (count > 0 && lastChar == ch)
{
++count;
}
else
{
if (count > 0)
{
sb.Append(count);
sb.Append(lastChar);
}
lastChar = ch;
count = 1;
}
}
if (count > 0)
{
sb.Append(count);
sb.Append(lastChar);
}
s = sb.ToString();
--n;
}
return s;
}
}
class Solution {
/**
* @param integer $n
* @return string
*/
function countAndSay($n) {
if ($n <= 0) {
return '';
}
$result = '1';
for ($i = 2; $i <= $n; $i++) {
$count = 1;
$say = '';
for ($j = 1; $j < strlen($result); $j++) {
if ($result[$j] == $result[$j - 1]) {
$count++;
} else {
$say .= $count . $result[$j - 1];
$count = 1;
}
}
$say .= $count . $result[strlen($result) - 1];
$result = $say;
}
return $result;
}
}