Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間1年前 (2024/09/24 09:04), 編輯推噓0(002)
留言2則, 1人參與, 1年前最新討論串899/1554 (看更多)
3043. longest common prefix 數字好多 懶得思考直接開trie== 感覺也可以直接用lca那套 還是等等用BIT試試 class Trie{ public: vector<Trie*> num; Trie(){ num = vector<Trie*>(10, nullptr); } }; class Solution { public: Trie* root = new Trie(); int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) { for(int& i: arr1){ Trie* t = root; string s = to_string(i); for(char& c: s){ int idx = c - '0'; if(t->num[idx] == nullptr) t->num[idx] = new Trie(); t = t->num[idx]; } } int maxlen = 0; for(int& i: arr2){ Trie* t = root; string s = to_string(i); int curlen = 0; for(char& c: s){ int idx = c - '0'; if(t->num[idx] == nullptr) break; curlen++; t = t->num[idx]; } maxlen = max(maxlen, curlen); } return maxlen; } }; ----- Sent from JPTT on my iPad -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.121.194 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1727139894.A.CDF.html

09/24 09:10, 1年前 , 1F
不對 完全沒必要 他又沒要找lca==
09/24 09:10, 1F

09/24 09:10, 1年前 , 2F
感覺還是trie一下就好
09/24 09:10, 2F
文章代碼(AID): #1cyX0spV (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cyX0spV (Marginalman)