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 798da96

Browse files
✨feat: Add 318
1 parent 4073c12 commit 798da96

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

‎Index/位运算.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| [231. 2 的幂](https://leetcode-cn.com/problems/power-of-two/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-two/solution/gong-shui-san-xie-2-de-mi-by-ac_oier-qm6e/) | 简单 | 🤩🤩🤩 |
77
| [260. 只出现一次的数字 III](https://leetcode-cn.com/problems/single-number-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-number-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-zgi4/) | 中等 | 🤩🤩🤩🤩 |
88
| [268. 丢失的数字](https://leetcode-cn.com/problems/missing-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/missing-number/solution/gong-shui-san-xie-yi-ti-wu-jie-pai-xu-ji-te3s/) | 简单 | 🤩🤩🤩🤩 |
9+
| [318. 最大单词长度乘积](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/gong-shui-san-xie-jian-dan-wei-yun-suan-cqtxq/) | 中等 | 🤩🤩🤩🤩 |
910
| [338. 比特位计数](https://leetcode-cn.com/problems/counting-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/counting-bits/solution/po-su-jie-fa-dong-tai-gui-hua-jie-fa-by-vvail/) | 简单 | 🤩🤩🤩 |
1011
| [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-four/solution/gong-shui-san-xie-zhuan-hua-wei-2-de-mi-y21lq/) | 简单 | 🤩🤩🤩 |
1112
| [371. 两整数之和](https://leetcode-cn.com/problems/sum-of-two-integers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-two-integers/solution/gong-shui-san-xie-shi-yong-wei-yun-suan-4hpb7/) | 中等 | 🤩🤩🤩🤩 |

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| [260. 只出现一次的数字 III](https://leetcode-cn.com/problems/single-number-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-number-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-zgi4/) | 中等 | 🤩🤩🤩🤩 |
1616
| [268. 丢失的数字](https://leetcode-cn.com/problems/missing-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/missing-number/solution/gong-shui-san-xie-yi-ti-wu-jie-pai-xu-ji-te3s/) | 简单 | 🤩🤩🤩🤩 |
1717
| [299. 猜数字游戏](https://leetcode-cn.com/problems/bulls-and-cows/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/bulls-and-cows/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-tdhs/) | 中等 | 🤩🤩🤩🤩 |
18+
| [318. 最大单词长度乘积](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/gong-shui-san-xie-jian-dan-wei-yun-suan-cqtxq/) | 中等 | 🤩🤩🤩🤩 |
1819
| [447. 回旋镖的数量](https://leetcode-cn.com/problems/number-of-boomerangs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-boomerangs/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-evu2/) | 中等 | 🤩🤩🤩🤩 |
1920
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
2021
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| [273. 整数转换英文表示](https://leetcode-cn.com/problems/integer-to-english-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/integer-to-english-words/solution/gong-shui-san-xie-zi-fu-chuan-da-mo-ni-b-0my6/) | 困难 | 🤩🤩🤩🤩 |
3232
| [284. 顶端迭代器](https://leetcode-cn.com/problems/peeking-iterator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/peeking-iterator/solution/gong-shui-san-xie-die-dai-qi-ji-ben-ren-b77lz/) | 中等 | 🤩🤩🤩🤩 |
3333
| [299. 猜数字游戏](https://leetcode-cn.com/problems/bulls-and-cows/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/bulls-and-cows/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-tdhs/) | 中等 | 🤩🤩🤩🤩 |
34+
| [318. 最大单词长度乘积](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/gong-shui-san-xie-jian-dan-wei-yun-suan-cqtxq/) | 中等 | 🤩🤩🤩🤩 |
3435
| [335. 路径交叉](https://leetcode-cn.com/problems/self-crossing/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-crossing/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-zdrb/) | 困难 | 🤩🤩🤩🤩 |
3536
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
3637
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[318. 最大单词长度乘积](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/gong-shui-san-xie-jian-dan-wei-yun-suan-cqtxq/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「位运算」、「哈希表」
6+
7+
8+
9+
给定一个字符串数组 `words`,找到 `length(word[i]) * length(word[j])` 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0ドル$。
10+
11+
示例 1:
12+
```
13+
输入: ["abcw","baz","foo","bar","xtfn","abcdef"]
14+
15+
输出: 16
16+
17+
解释: 这两个单词为 "abcw", "xtfn"。
18+
```
19+
示例 2:
20+
```
21+
输入: ["a","ab","abc","d","cd","bcd","abcd"]
22+
23+
输出: 4
24+
25+
解释: 这两个单词为 "ab", "cd"。
26+
```
27+
示例 3:
28+
```
29+
输入: ["a","aa","aaa","aaaa"]
30+
31+
输出: 0
32+
33+
解释: 不存在这样的两个单词。
34+
```
35+
36+
提示:
37+
* 2 <= words.length <= 1000
38+
* 1 <= words[i].length <= 1000
39+
* words[i] 仅包含小写字母
40+
41+
---
42+
43+
### 模拟
44+
45+
根据题意进行模拟即可,利用每个 $words[i]$ 只有小写字母,且只需要区分两字符是否有字母重复。
46+
47+
我们可以使用一个 `int` 来代指某个 $word[i]$:低 26ドル$ 来代指字母 `a-z` 是否出现过。
48+
49+
然后对每个「字符对」所对应的两个 `int` 值执行 `&` 操作(若两字符无重复字符,则结果为 0ドル$),并得出最终答案。
50+
51+
代码:
52+
```Java
53+
class Solution {
54+
public int maxProduct(String[] words) {
55+
int n = words.length, idx = 0;
56+
int[] masks = new int[n];
57+
for (String w : words) {
58+
int t = 0;
59+
for (int i = 0; i < w.length(); i++) {
60+
int u = w.charAt(i) - 'a';
61+
t |= (1 << u);
62+
}
63+
masks[idx++] = t;
64+
}
65+
int ans = 0;
66+
for (int i = 0; i < n; i++) {
67+
for (int j = 0; j < i; j++) {
68+
if ((masks[i] & masks[j]) == 0) ans = Math.max(ans, words[i].length() * words[j].length());
69+
}
70+
}
71+
return ans;
72+
}
73+
}
74+
```
75+
* 时间复杂度:令 $n$ 为 $words$ 数组的长度,转换出 $masks$ 的复杂度为 $O(\sum_{i = 0}^{i = n - 1}words[i].length)$;得到答案的复杂度为 $O(n^2)$。整体复杂度为 $O(\max(\sum_{i = 0}^{i = n - 1}words[i].length, n^2))$
76+
* 空间复杂度:$O(n)$
77+
78+
---
79+
80+
### 优化
81+
82+
不难发现,对于词频相同($mask$ 值相等)的两字符,只需要保留字符长度大的即可,因此我们可以使用「哈希表」代替 $masks$ 数组。
83+
84+
代码:
85+
```Java
86+
class Solution {
87+
public int maxProduct(String[] words) {
88+
Map<Integer, Integer> map = new HashMap<>();
89+
for (String w : words) {
90+
int t = 0, m = w.length();
91+
for (int i = 0; i < m; i++) {
92+
int u = w.charAt(i) - 'a';
93+
t |= (1 << u);
94+
}
95+
if (!map.containsKey(t) || map.get(t) < m) map.put(t, m);
96+
}
97+
int ans = 0;
98+
for (int a : map.keySet()) {
99+
for (int b : map.keySet()) {
100+
if ((a & b) == 0) ans = Math.max(ans, map.get(a) * map.get(b));
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
* 时间复杂度:令 $n$ 为 $words$ 数组的长度,转换出 $masks$ 的复杂度为 $O(\sum_{i = 0}^{i = n - 1}words[i].length)$;得到答案的复杂度为 $O(n^2)$。整体复杂度为 $O(\max(\sum_{i = 0}^{i = n - 1}words[i].length, n^2))$
108+
* 空间复杂度:$O(n)$
109+
110+
---
111+
112+
### 最后
113+
114+
这是我们「刷穿 LeetCode」系列文章的第 `No.318` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
(0)

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