Re: [閒聊] 每日leetcode

看板Marginalman作者 (溫水佳樹的兄長大人)時間1年前 (2024/09/12 08:44), 編輯推噓0(001)
留言1則, 1人參與, 1年前最新討論串847/1548 (看更多)
※ 引述《enmeitiryous (enmeitiryous)》之銘言: : 今天是easy : 題目:1684. count the number of consistent strings : 給你一個字串allowed並給你一個由字串組成的vector,求在這個vector中有多少個字串 : 其中的字母都出現在字串allowed裡面 : 思路: : 用aloowed建unordered-set然後每次確認遇到的字串是否符合條件,如果是的話則ans++ : 作法基本上大同小異,topic有標bit maniuplation,可以看一下editorial有利用bit去 : 做到比對字母有沒在allow裡面,蠻酷的 : int countConsistentStrings(string allowed, vector<string>& words) { : unordered_set<char> lk; : bool tt=false; : int ans=0; : for(auto k:allowed){ : lk.insert(k); : } : for(auto j:words){ : tt=true; : for(auto g:j){ : if(!lk.count(g)){ : tt=false; : } : } : if(tt){ : ans++; : } : } : return ans; : } 思路: 就一個字一個字確認 好像也沒更快的方法 Python Code: class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: result = 0 for word in words: result += 1 for c in word: if c not in allowed: result -= 1 break return result -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.140.135.204 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726101874.A.8AF.html

09/12 08:50, 1年前 , 1F
早早
09/12 08:50, 1F
文章代碼(AID): #1cuZboYl (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cuZboYl (Marginalman)