Tag : 「模擬」、「哈希表」

網(wǎng)站域名 "discuss.leetcode.com" 由多個子域名組成。頂級域名為 "com" ,二級域名為 "leetcode.co" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁 > 營銷資訊 > 建站知識 > 811. 子域名訪問計數(shù) : 簡單哈希表運用題

811. 子域名訪問計數(shù) : 簡單哈希表運用題

時間:2023-02-03 19:52:01 | 來源:建站知識

時間:2023-02-03 19:52:01 來源:建站知識

題目描述

這是 LeetCode 上的 811. 子域名訪問計數(shù) ,難度為 中等。

Tag : 「模擬」、「哈希表」

網(wǎng)站域名 "discuss.leetcode.com" 由多個子域名組成。頂級域名為 "com" ,二級域名為 "leetcode.com" ,最低一級為 "discuss.leetcode.com" 。

當訪問域名 "discuss.leetcode.com" 時,同時也會隱式訪問其父域名 "leetcode.com" 以及 "com" 。

計數(shù)配對域名 是遵循 "rep d1.d2.d3""rep d1.d2" 格式的一個域名表示,其中 rep 表示訪問域名的次數(shù),d1.d2.d3 為域名本身。

例如,"9001 discuss.leetcode.com" 就是一個 計數(shù)配對域名 ,表示 discuss.leetcode.com 被訪問了 9001 次。

給你一個 計數(shù)配對域名 組成的數(shù)組 cpdomains ,解析得到輸入中每個子域名對應的 計數(shù)配對域名 ,并以數(shù)組形式返回??梢园?任意順序 返回答案。

示例 1:

輸入:cpdomains = ["9001 discuss.leetcode.com"]輸出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]解釋:例子中僅包含一個網(wǎng)站域名:"discuss.leetcode.com"。按照前文描述,子域名 "leetcode.com" 和 "com" 都會被訪問,所以它們都被訪問了 9001 次。示例 2:

輸入:cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]輸出:["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]解釋:按照前文描述,會訪問 "google.mail.com" 900 次,"yahoo.com" 50 次,"intel.mail.com" 1 次,"wiki.org" 5 次。而對于父域名,會訪問 "mail.com" 900 + 1 = 901 次,"com" 900 + 50 + 1 = 951 次,和 "org" 5 次。提示:

哈希表

為了方便,我們令 cpdomainsss。

根據(jù)題意進行模擬:使用哈希表記錄每個域名的總訪問次數(shù),從前往后處理所有的 ss[i]。在處理某個 ss[i] 時(記長度為 n,使用指針 idx 代指掃描到的游標位置),先通過指針掃描找到訪問數(shù)字部分 cnt = ss[i][0:idx],然后「從后往前」處理 ss[i][idx + 1, n - 1] 部分,按照域名層級「從小到大」的順序進行截取,并累加訪問次數(shù) cnt 到當前域名。

最后根據(jù)哈希表構(gòu)造答案。

Java 代碼:

class Solution { public List<String> subdomainVisits(String[] ss) { Map<String, Integer> map = new HashMap<>(); for (String s : ss) { int n = s.length(), idx = 0; while (idx < n && s.charAt(idx) != ' ') idx++; int cnt = Integer.parseInt(s.substring(0, idx)); int start = idx + 1; idx = n - 1; while (idx >= start) { while (idx >= start && s.charAt(idx) != '.') idx--; String cur = s.substring(idx + 1); map.put(cur, map.getOrDefault(cur, 0) + cnt); idx--; } } List<String> ans = new ArrayList<>(); for (String key : map.keySet()) ans.add(map.get(key) + " " + key); return ans; }}TypeScript 代碼:

function subdomainVisits(ss: string[]): string[] { const map = new Map<string, number>() for (const s of ss) { let n = s.length, idx = 0 while (idx < n && s[idx] != ' ') idx++ const cnt = Number(s.substring(0, idx)) const start = idx + 1; idx = n - 1 while (idx >= start) { while (idx >= start && s[idx] != '.') idx-- const cur = s.substring(idx + 1) if (!map.has(cur)) map.set(cur, 0) map.set(cur, map.get(cur) + cnt) idx-- } } const ans = new Array<string>() for (const key of map.keys()) ans.push(map.get(key) + " " + key) return ans};Python 代碼:

class Solution: def subdomainVisits(self, ss: List[str]) -> List[str]: mapping = defaultdict(int) for s in ss: n, idx = len(s), 0 while idx < n and s[idx] != ' ': idx += 1 cnt = int(s[:idx]) start, idx = idx + 1, n - 1 while idx >= start: while idx >= start and s[idx] != '.': idx -= 1 mapping[s[idx + 1:]] += cnt idx -= 1 return [f'{v} {k}' for k, v in mapping.items()]

最后

這是我們「刷穿 LeetCode」系列文章的第 No.811 篇,系列開始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道題目,部分是有鎖題,我們將先把所有不帶鎖的題目刷完。

在這個系列文章里面,除了講解解題思路以外,還會盡可能給出最為簡潔的代碼。如果涉及通解還會相應的代碼模板。

為了方便各位同學能夠電腦上進行調(diào)試和提交代碼,我建立了相關的倉庫:https://github.com/SharingSource/LogicStack-LeetCode 。

在倉庫地址里,你可以看到系列文章的題解鏈接、系列文章的相應代碼、LeetCode 原題鏈接和其他優(yōu)選題解。

更多更全更熱門的「筆試/面試」相關資料可訪問排版精美的 合集新基地

關鍵詞:運用,簡單,訪問

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉