Skip to content

Commit d3c81c3

Browse files
committed
✨feat: add 剑指题目
1 parent 82ddaaa commit d3c81c3

9 files changed

+340
-16
lines changed

Index/位运算.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |
2424
| [剑指 Offer 15. 二进制中1的个数](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/solution/gong-shui-san-xie-yi-ti-si-jie-wei-shu-j-g9w6/) | 简单 | 🤩🤩🤩 |
2525
| [剑指 Offer II 003. 前 n 个数字二进制中 1 的个数](https://leetcode.cn/problems/w3tCBm/) | [LeetCode 题解链接](https://leetcode.cn/problems/w3tCBm/solution/by-ac_oier-cnlt/) | 简单 | 🤩🤩🤩 |
26+
| [剑指 Offer II 005. 单词长度的最大乘积](https://leetcode.cn/problems/aseY1I/) | [LeetCode 题解链接](https://leetcode.cn/problems/aseY1I/solution/by-ac_oier-qk3a/) | 中等 | 🤩🤩🤩🤩 |
2627

Index/字典树.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
| [720. 词典中最长的单词](https://leetcode-cn.com/problems/longest-word-in-dictionary/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-word-in-dictionary/solution/by-ac_oier-bmot/) | 简单 | 🤩🤩🤩🤩 |
1111
| [745. 前缀和后缀搜索](https://leetcode.cn/problems/prefix-and-suffix-search/) | [LeetCode 题解链接](https://leetcode.cn/problems/prefix-and-suffix-search/solution/by-ac_oier-ayej/) | 困难 | 🤩🤩🤩🤩 |
1212
| [1707. 与数组中元素的最大异或值](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/solution/gong-shui-san-xie-jie-zhe-ge-wen-ti-lai-lypqr/) | 困难 | 🤩🤩🤩 |
13+
| [剑指 Offer II 067. 最大的异或](https://leetcode.cn/problems/ms70jA/) | [LeetCode 题解链接](https://leetcode.cn/problems/ms70jA/solution/by-ac_oier-d9kx/) | 中等 | 🤩🤩🤩🤩 |
1314

Index/模拟.md

+2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@
187187
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
188188
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
189189
| [面试题 17.11. 单词距离](https://leetcode.cn/problems/find-closest-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-closest-lcci/solution/by-ac_oier-0hv9/) | 中等 | 🤩🤩🤩🤩 |
190+
| [剑指 Offer II 003. 前 n 个数字二进制中 1 的个数](https://leetcode.cn/problems/w3tCBm/) | [LeetCode 题解链接](https://leetcode.cn/problems/w3tCBm/solution/by-ac_oier-cnlt/) | 简单 | 🤩🤩🤩 |
191+
| [剑指 Offer II 005. 单词长度的最大乘积](https://leetcode.cn/problems/aseY1I/) | [LeetCode 题解链接](https://leetcode.cn/problems/aseY1I/solution/by-ac_oier-qk3a/) | 中等 | 🤩🤩🤩🤩 |
190192
| [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 |
191193
| [剑指 Offer II 041. 滑动窗口的平均值](https://leetcode.cn/problems/qIsx9U/) | [LeetCode 题解链接](https://leetcode.cn/problems/qIsx9U/solution/by-ac_oier-g5ha/) | 简单 | 🤩🤩🤩🤩 |
192194

Index/贪心算法.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@
4040
| [1877. 数组中最大数对和的最小值](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ru29y/) | 中等 | 🤩🤩🤩🤩🤩 |
4141
| [1996. 游戏中弱角色的数量](https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game/solution/gong-shui-san-xie-tan-xin-yun-yong-ti-by-5ok6/) | 中等 | 🤩🤩🤩🤩 |
4242
| [2170. 使数组变成交替数组的最少操作数](https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-alternating/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-alternating/solution/gong-shui-san-xie-jian-dan-ji-shu-lei-ta-0eo4/) | 中等 | 🤩🤩🤩🤩 |
43+
| [剑指 Offer II 067. 最大的异或](https://leetcode.cn/problems/ms70jA/) | [LeetCode 题解链接](https://leetcode.cn/problems/ms70jA/solution/by-ac_oier-d9kx/) | 中等 | 🤩🤩🤩🤩 |
4344

LeetCode/131-140/137. 只出现一次的数字 II(中等).md

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Tag : 「哈希表」、「位运算」
66

77

88

9-
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
10-
9+
给你一个整数数组 `nums`,除某个元素仅出现一次外,其余每个元素都恰出现三次。请你找出并返回那个只出现了一次的元素。
1110

1211
示例 1:
1312
```
@@ -23,18 +22,12 @@ Tag : 「哈希表」、「位运算」
2322
```
2423

2524
提示:
26-
* 1 <= nums.length <= 3 * $10^4$
27-
28-
* -$2^{31}$ <= nums[i] <= $2^{31}$ - 1
29-
30-
* nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
31-
32-
25+
* $1 <= nums.length <= 3 \times 10^4$
26+
* $-2^{31} <= nums[i] <= 2^{31} - 1$
27+
* `nums` 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
3328

3429
进阶:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
3530

36-
37-
3831
---
3932

4033
### 哈希表
@@ -137,7 +130,6 @@ class Solution {
137130
* 时间复杂度:$O(n)$
138131
* 空间复杂度:$O(1)$
139132

140-
141133
---
142134

143135
### 最后

LeetCode/1421-1430/1422. 分割字符串的最大得分(简单).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function maxScore(s: string): number {
109109
let n = s.length, cur = s[0] == '0' ? 1 : 0
110110
for (let i = 1; i < n; i++) cur += s[i] == '1' ? 1 : 0
111111
let ans = cur
112-
for (let i = 2; i < n - 1; i++) {
112+
for (let i = 1; i < n - 1; i++) {
113113
cur += s[i] == '0' ? 1 : -1
114114
ans = Math.max(ans, cur)
115115
}

LeetCode/421-430/421. 数组中两个数的最大异或值(中等).md

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ Tag : 「字典树」、「贪心」
4444
```
4545

4646
提示:
47-
* 1 <= nums.length <= 2 * $10^4$
48-
* 0 <= nums[i] <= $2^{31}$ - 1
47+
* $1 <= nums.length <= 2 \times 10^4$
48+
* $0 <= nums[i] <= 2^{31} - 1$
4949

5050
---
5151

52-
### 分析
52+
### 基本分析
5353

5454
要求得数组 `nums` 中的「最大异或结果」,假定 $nums[i]$ 与 $nums[j]$ 异或可以取得最终结果。
5555

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[剑指 Offer II 005. 单词长度的最大乘积](https://leetcode.cn/problems/aseY1I/solution/by-ac_oier-qk3a/)** ,难度为 **中等**
4+
5+
Tag :「模拟」、「位运算」
6+
7+
8+
9+
给定一个字符串数组 `words`,请计算当两个字符串 `words[i]``words[j]` 不包含相同字符时,它们长度的乘积的最大值。假设字符串中只包含英语的小写字母。如果没有不包含相同字符的一对字符串,返回 $0$。
10+
11+
示例 1:
12+
```
13+
输入: words = ["abcw","baz","foo","bar","fxyz","abcdef"]
14+
15+
输出: 16
16+
17+
解释: 这两个单词为 "abcw", "fxyz"。它们不包含相同字符,且长度的乘积最大。
18+
```
19+
示例 2:
20+
```
21+
输入: words = ["a","ab","abc","d","cd","bcd","abcd"]
22+
23+
输出: 4
24+
25+
解释: 这两个单词为 "ab", "cd"。
26+
```
27+
示例 3:
28+
```
29+
输入: words = ["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$ 数组的长度,得到 $map$ 的复杂度为 $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」系列文章的第 `剑指 Offer II 005` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
 (0)