Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間1年前 (2024/08/03 04:24), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串621/1554 (看更多)
224. 小計算機parser 學康拍樂做的小作業都比這個複雜== 以前的hard真的都很純 意思到了 原本想說用stack 進進又退退的感覺不是很爽 後來想說建樹 全部都擺起來怕太大 沒有要選市長不用這種建樹吧 用同一套邏輯記state 邊走邊算 好像又有點像stack了 樹位邏輯 using ll = long long; class Solution { public: int calculate(string s) { ll res = 0; stack<pair<ll, bool>> st; bool type = 1; // false:'-', true:'+' //<value, type> ll cur = 0; for(char c: s){ if(c == ' ') continue; if(c == '(') { st.push({res, type}); res = 0; type = true; } else if(c == ')') { cal(res, type, cur); cur = res; res = st.top().first; type = st.top().second; cal(res, type, cur); st.pop(); } else{ if(c == '-'){ cal(res, type, cur); type = false; } else if(c == '+'){ cal(res, type, cur); type = true; } else{ //digits cur *= 10; cur += (c - '0'); } } } cal(res, type, cur); return (int)res; } void cal(ll& res, bool& type, ll& cur){ if(type) res += cur; else res -= cur; type = true; cur = 0; //cout << res << '\n'; } }; -- 我看起來笨笨的 而且我的扣好醜好醜 還賺不到錢 https://i.imgur.com/eo8jQaL.jpeg
----- Sent from JPTT on my iPad -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.121.194 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722630268.A.057.html

08/03 06:37, 1年前 , 1F
大師
08/03 06:37, 1F
文章代碼(AID): #1chK1y1N (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1chK1y1N (Marginalman)