Re: [閒聊] 每日leetcode

看板Marginalman作者 (南爹摳打)時間1年前 (2024/04/07 15:26), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串100/1548 (看更多)
想法差不多 但我一開始最後面只判斷個數沒檢查index (就是直接return star.Count>=left.Count) 想說為什麼都過不了 修正之後就過了 大神好強 C# code: public class Solution { public bool CheckValidString(string s) { var left = new Stack<int>(); var star = new Stack<int>(); int length = s.Length; for (int i=0; i<length; i++) { if (s[i] == '(') { left.Push(i); continue; } if (s[i] == '*') { star.Push(i); continue; } if (left.Count > 0) { left.Pop(); continue; } if (star.Count > 0) { star.Pop(); continue; } return false; } if (left.Count > star.Count) return false; while (left.Count != 0) { if (left.Pop() > star.Pop()) return false; } return true; } } -- (づ′・ω・)づ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.160.52 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1712474771.A.7D3.html

04/07 15:28, 1年前 , 1F
大師,別捲了
04/07 15:28, 1F
文章代碼(AID): #1c4agJVJ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1c4agJVJ (Marginalman)