Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (caster )時間1年前 (2024/04/05 18:04), 編輯推噓2(204)
留言6則, 6人參與, 1年前最新討論串95/1548 (看更多)
※ 引述《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) 補字補字補 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.168.233 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1712311443.A.228.html

04/05 18:05, 1年前 , 1F
大師
04/05 18:05, 1F

04/05 18:05, 1年前 , 2F
大師
04/05 18:05, 2F

04/05 18:06, 1年前 , 3F
你才大師 index的方法比較難寫
04/05 18:06, 3F

04/05 18:09, 1年前 , 4F
大師
04/05 18:09, 4F

04/05 18:24, 1年前 , 5F
大師
04/05 18:24, 5F

04/05 18:25, 1年前 , 6F
大師
04/05 18:25, 6F
文章代碼(AID): #1c3yoJ8e (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1c3yoJ8e (Marginalman)