Re: [閒聊] 每日leetcode

作者: devilkool (對貓毛過敏的貓控)   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();
}

Links booklink

Contact Us: admin [ a t ] ucptt.com