Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間1年前 (2024/12/02 22:48), 編輯推噓2(207)
留言9則, 3人參與, 1年前最新討論串1173/1548 (看更多)
我一開始看也想說 阿就字典樹隨便丟 後來看一下easy 喔應該掃一遍就沒了ㄅ class Solution { public: int isPrefixOfWord(string s, string w) { int n = s.length(), m = w.length(); int cnt = 1; for(int i = 0, check = 1; i < n; ){ if(!check){ while(i < n and s[i] != ' ') i++; check = true; i++; cnt++; } for(int j = 0; j < m and i < n; j++, i++){ if(s[i] != w[j]){ check = false; break; } if(j == m-1) return cnt; } } return -1; } }; 寫完還是覺得字典樹比較簡單 又寫了一遍== class trie{ public: vector<trie*> ch; int idx; trie(): ch(vector<trie*>(26, nullptr)), idx(0) {}; }; class Solution { public: int isPrefixOfWord(string s, string w) { int idx = 1; trie* root = new trie(); trie* cur = root; for(char& c: s){ if(c == ' ') { cur = root; idx++; } else{ if(cur->ch[c-'a'] == nullptr){ cur->ch[c-'a'] = new trie(); } cur = cur->ch[c-'a']; if(!cur->idx) cur->idx = idx; } } for(char& c: w){ if(!root) return -1; root = root->ch[c-'a']; } if(!root) return -1; return root->idx; } }; ※ 引述《DJYOMIYAHINA (通通打死)》之銘言: : 想說要用trie什麼之類的 : 看到綠色的標題 : 我就懶了 : 我好爛 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.51.137.241 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1733150939.A.DA1.html

12/02 22:49, 1年前 , 1F
所以redeem到底能不能救上個月的
12/02 22:49, 1F

12/02 22:54, 1年前 , 2F
應該可以 不過我覺得那個徽章沒什麼用欸
12/02 22:54, 2F

12/02 22:54, 1年前 , 3F
還不如存著換衣服
12/02 22:54, 3F

12/02 22:56, 1年前 , 4F
沒有什麼連續簽到100天獎勵的嗎
12/02 22:56, 4F

12/02 22:57, 1年前 , 5F
好吧 存著換衣服好了
12/02 22:57, 5F

12/02 22:57, 1年前 , 6F
oin大大說的都是對的 聽你的
12/02 22:57, 6F

12/02 22:57, 1年前 , 7F
連續一個月有 我今天拿到一千天的徽章 但不是連續就是了
12/02 22:57, 7F

12/02 23:13, 1年前 , 8F
嘿嘿 你這樣說 我好害羞喔
12/02 23:13, 8F

12/03 09:35, 1年前 , 9F
你好怪
12/03 09:35, 9F
文章代碼(AID): #1dJSZRsX (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dJSZRsX (Marginalman)