作者devilkool (可可粉)
標題Re: [閒聊] 每日leetcode
時間2024-11-05 01:42:25
只有這種跟easy沒兩樣的medium才寫得出來了
3163. String Compression III
public string CompressedString(string word)
{
if (word.Length == 1)
{
return $"1{word}";
}
var result = new StringBuilder();
var lastCharacter = word[0];
var lastCount = 1;
for (int i=1;i<word.Length;i++)
{
if (word[i] == word[i-1] & lastCount < 9)
{
lastCount++;
}
else
{
result.Append($"{lastCount}{word[i - 1]}");
lastCount = 1;
lastCharacter = word[i];
}
}
result.Append($"{lastCount}{word[word.Length-1]}");
return result.ToString();
}
--
※ 發信站: 批踢踢實業坊(ptt-website.tw), 來自: 36.230.58.252 (臺灣)
※ 文章網址: https://ptt-website.tw/Marginalman/M.1730742148.A.0BE