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

看板Marginalman作者 (是oin的說)時間1年前 (2024/07/12 12:44), 編輯推噓2(204)
留言6則, 6人參與, 1年前最新討論串476/1548 (看更多)
題目: 給你一個字串 你可以消除中間的ab得到x分 或是消除中間的ba得到y分 問你最多能得幾分 思路: 題目的重點就是 像是aba 如果x>y就要選擇ab的組合 得到x分 然後消除ab 不然就是ba 所以需要對兩種情況做stack 然後要stack兩次 寫成函式之後比較簡潔了 ```cpp class Solution { public: string test(string s, char l, char r) { int len = s.size(); vector<char> res; for(int i = 0 ; i < len ; i ++) { res.push_back(s[i]); while(res.size()>1 && res[res.size()-2] == l && res[res.size()-1] == r) { res.pop_back(); res.pop_back(); } } string res2(res.begin(),res.end()); return res2; } int maximumGain(string s, int x, int y) { int res = 0; if(x > y) { string ab = test(s,'a','b'); res += (s.size()-ab.size())/2 * x; string ba = test(ab,'b','a'); res += (ab.size()-ba.size())/2 * y; } else { string ba = test(s,'b','a'); res += (s.size()-ba.size())/2 * y; string ab = test(ba,'a','b'); res += (ba.size()-ab.size())/2 * x; } return res; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.13.212 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720759462.A.47A.html

07/12 12:45, 1年前 , 1F
我好崇拜你
07/12 12:45, 1F

07/12 12:45, 1年前 , 2F
你什麼時後要刷題 操
07/12 12:45, 2F

07/12 12:47, 1年前 , 3F
給你一個OIN 你可以幹他得到X 或是被他幹得Y分
07/12 12:47, 3F

07/12 12:52, 1年前 , 4F
大師
07/12 12:52, 4F

07/12 12:55, 1年前 , 5F
我好崇拜你,未來姑姑魯
07/12 12:55, 5F

07/12 13:04, 1年前 , 6F
大師 幫內推
07/12 13:04, 6F
文章代碼(AID): #1caBIcHw (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1caBIcHw (Marginalman)