精華區beta Marginalman 關於我們 聯絡資訊
※ 引述《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
JIWP: 大師 04/05 18:05
amam1111: 大師 04/05 18:05
sustainer123: 你才大師 index的方法比較難寫 04/05 18:06
digua: 大師 04/05 18:09
SecondRun: 大師 04/05 18:24
wwndbk: 大師 04/05 18:25