Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-04-05 18:04:01
※ 引述《SecondRun (南爹摳打)》之銘言:
: 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守門員
思路差不多 stack然後檢查
Python Code:
class Solution:
def makeGood(self, s: str) -> str:
stack = []
for e in s:
if stack and abs(ord(e)-ord(stack[-1])) == 32:
stack.pop()
else:
stack.append(e)
return "".join(stack)
補字補字補
作者: JIWP (JIWP)   2024-04-05 18:05:00
大師
作者: amam1111 (thousand)   2024-04-05 18:05:00
大師
作者: sustainer123 (caster)   2024-04-05 18:06:00
你才大師 index的方法比較難寫
作者: digua (地瓜)   2024-04-05 18:09:00
大師
作者: SecondRun (雨夜琴聲)   2024-04-05 18:24:00
大師
作者: wwndbk (黑人問號)   2024-04-05 18:25:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com