Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 879a663

Browse files
Merge pull request SharingSource#701 from SharingSource/ac_oier
✨feat: add 811
2 parents 4919149 + e8116c7 commit 879a663

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
4747
| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 |
4848
| [736. Lisp 语法解析](https://leetcode.cn/problems/parse-lisp-expression/) | [LeetCode 题解链接](https://leetcode.cn/problems/parse-lisp-expression/solution/by-ac_oier-i7w1/) | 困难 | 🤩🤩🤩🤩 |
49+
| [811. 子域名访问计数](https://leetcode.cn/problems/subdomain-visit-count/) | [LeetCode 题解链接](https://leetcode.cn/problems/subdomain-visit-count/solution/by-ac_oier-aex6/) | 中等 | 🤩🤩🤩🤩 |
4950
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
5051
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
5152
| [873. 最长的斐波那契子序列的长度](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/) | [LeetCode 题解链接](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-ac_oier-beo2/) | 中等 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
106106
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
107107
| [806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/) | 简单 | 🤩🤩🤩🤩 |
108+
| [811. 子域名访问计数](https://leetcode.cn/problems/subdomain-visit-count/) | [LeetCode 题解链接](https://leetcode.cn/problems/subdomain-visit-count/solution/by-ac_oier-aex6/) | 中等 | 🤩🤩🤩🤩 |
108109
| [812. 最大三角形面积](https://leetcode.cn/problems/largest-triangle-area/) | [LeetCode 题解链接](https://leetcode.cn/problems/largest-triangle-area/solution/by-ac_oier-htv8/) | 简单 | 🤩🤩🤩🤩 |
109110
| [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 |
110111
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[811. 子域名访问计数](https://leetcode.cn/problems/subdomain-visit-count/solution/by-ac_oier-aex6/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「哈希表」
6+
7+
8+
9+
网站域名 `"discuss.leetcode.com"` 由多个子域名组成。顶级域名为 `"com"` ,二级域名为 `"leetcode.com"` ,最低一级为 `"discuss.leetcode.com"`
10+
11+
当访问域名 `"discuss.leetcode.com"` 时,同时也会隐式访问其父域名 `"leetcode.com"` 以及 `"com"`
12+
13+
计数配对域名 是遵循 `"rep d1.d2.d3"``"rep d1.d2"` 格式的一个域名表示,其中 `rep` 表示访问域名的次数,`d1.d2.d3` 为域名本身。
14+
15+
例如,`"9001 discuss.leetcode.com"` 就是一个 计数配对域名 ,表示 `discuss.leetcode.com` 被访问了 `9001` 次。
16+
17+
给你一个 计数配对域名 组成的数组 `cpdomains` ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。
18+
19+
示例 1:
20+
```
21+
输入:cpdomains = ["9001 discuss.leetcode.com"]
22+
23+
输出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
24+
25+
解释:例子中仅包含一个网站域名:"discuss.leetcode.com"。
26+
按照前文描述,子域名 "leetcode.com" 和 "com" 都会被访问,所以它们都被访问了 9001 次。
27+
```
28+
示例 2:
29+
```
30+
输入:cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
31+
32+
输出:["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
33+
34+
解释:按照前文描述,会访问 "google.mail.com" 900 次,"yahoo.com" 50 次,"intel.mail.com" 1 次,"wiki.org" 5 次。
35+
而对于父域名,会访问 "mail.com" 900 + 1 = 901 次,"com" 900 +たす 50 +たす 1 = 951 次,和 "org" 5 次。
36+
```
37+
38+
提示:
39+
* 1ドル <= cpdomain.length <= 100$
40+
* 1ドル <= cpdomain[i].length <= 100$
41+
* `cpdomain[i]` 会遵循 `"repi d1i.d2i.d3i"``"repi d1i.d2i"` 格式
42+
* `repi` 是范围 $[1, 10^4]$ 内的一个整数
43+
* $d1_i$、$d2_i$ 和 $d3_i$ 由小写英文字母组成
44+
45+
---
46+
47+
### 哈希表
48+
49+
为了方便,我们令 `cpdomains``ss`
50+
51+
根据题意进行模拟:使用哈希表记录每个域名的总访问次数,从前往后处理所有的 $ss[i]$。在处理某个 $ss[i]$ 时(记长度为 $n,ドル使用指针 `idx` 代指扫描到的游标位置),先通过指针扫描找到访问数字部分 `cnt = ss[i][0:idx]`,然后「从后往前」处理 $ss[i]$ 的 $[idx + 1, n - 1]$ 部分,按照域名层级「从小到大」的顺序进行截取,并累加访问次数 `cnt` 到当前域名。
52+
53+
最后根据哈希表构造答案。
54+
55+
Java 代码:
56+
```Java
57+
class Solution {
58+
public List<String> subdomainVisits(String[] ss) {
59+
Map<String, Integer> map = new HashMap<>();
60+
for (String s : ss) {
61+
int n = s.length(), idx = 0;
62+
while (idx < n && s.charAt(idx) != ' ') idx++;
63+
int cnt = Integer.parseInt(s.substring(0, idx));
64+
int start = idx + 1; idx = n - 1;
65+
while (idx >= start) {
66+
while (idx >= start && s.charAt(idx) != '.') idx--;
67+
String cur = s.substring(idx + 1);
68+
map.put(cur, map.getOrDefault(cur, 0) + cnt);
69+
idx--;
70+
}
71+
}
72+
List<String> ans = new ArrayList<>();
73+
for (String key : map.keySet()) ans.add(map.get(key) + " " + key);
74+
return ans;
75+
}
76+
}
77+
```
78+
TypeScript 代码:
79+
```TypeScript
80+
function subdomainVisits(ss: string[]): string[] {
81+
const map = new Map<string, number>()
82+
for (const s of ss) {
83+
let n = s.length, idx = 0
84+
while (idx < n && s[idx] != ' ') idx++
85+
const cnt = Number(s.substring(0, idx))
86+
const start = idx + 1; idx = n - 1
87+
while (idx >= start) {
88+
while (idx >= start && s[idx] != '.') idx--
89+
const cur = s.substring(idx + 1)
90+
if (!map.has(cur)) map.set(cur, 0)
91+
map.set(cur, map.get(cur) + cnt)
92+
idx--
93+
}
94+
}
95+
const ans = new Array<string>()
96+
for (const key of map.keys()) ans.push(map.get(key) + " " + key)
97+
return ans
98+
};
99+
```
100+
Python 代码:
101+
```Python
102+
class Solution:
103+
def subdomainVisits(self, ss: List[str]) -> List[str]:
104+
mapping = defaultdict(int)
105+
for s in ss:
106+
n, idx = len(s), 0
107+
while idx < n and s[idx] != ' ':
108+
idx += 1
109+
cnt = int(s[:idx])
110+
start, idx = idx + 1, n - 1
111+
while idx >= start:
112+
while idx >= start and s[idx] != '.':
113+
idx -= 1
114+
mapping[s[idx + 1:]] += cnt
115+
idx -= 1
116+
return [f'{v}{k}' for k, v in mapping.items()]
117+
```
118+
* 时间复杂度:$O(\sum_{i = 0}^{n - 1}len(ss[i]))$
119+
* 空间复杂度:$O(\sum_{i = 0}^{n - 1}len(ss[i]))$
120+
121+
---
122+
123+
### 最后
124+
125+
这是我们「刷穿 LeetCode」系列文章的第 `No.811` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
126+
127+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
128+
129+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
130+
131+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
132+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /