Re: [閒聊] 每日leetcode

作者: SecondRun (雨夜琴聲)   2024-04-05 12:32:10
1544. Make The String Great
input string假如有相鄰的同樣字母的大小寫,移除這組字母
重複這個動作直到沒有相鄰大小寫
想法:移除了之後還要檢查移除組的左右,用index很麻煩所以用stack
C# code:
public class Solution {
public string MakeGood(string s) {
if (s.Length <= 1) return s;
var stack = new Stack<char>();
foreach (char c in s)
{
if (stack.Count != 0 && Math.Abs(c - stack.Peek()) == 32)
{
stack.Pop();
continue;
}
stack.Push(c);
}
var result = string.Empty;
while (stack.Count != 0)
{
result = stack.Pop() + result;
}
return result;
}
}
我是EASY守門員
作者: sustainer123 (caster)   2024-04-05 12:37:00
大師
作者: JIWP (JIWP)   2024-04-05 13:21:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com