Skip to content

Commit 5d4b5dc

Browse files
committed
feat: update lc problems
1 parent 6732085 commit 5d4b5dc

File tree

22 files changed

+291
-94
lines changed

22 files changed

+291
-94
lines changed

solution/0100-0199/0136.Single Number/README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,46 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个<strong>非空</strong>整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
9+
<p>给你一个 <strong>非空</strong> 整数数组 <code>nums</code> ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
1010

11-
<p><strong>说明:</strong></p>
11+
<p>你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。</p>
1212

13-
<p>你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?</p>
13+
<div class="original__bRMd">
14+
<div>
15+
<p>&nbsp;</p>
1416

15-
<p><strong>示例 1:</strong></p>
17+
<p><strong class="example">示例 1</strong></p>
1618

17-
<pre><strong>输入:</strong> [2,2,1]
18-
<strong>输出:</strong> 1
19+
<pre>
20+
<strong>输入:</strong>nums = [2,2,1]
21+
<strong>输出:</strong>1
1922
</pre>
2023

21-
<p><strong>示例&nbsp;2:</strong></p>
24+
<p><strong class="example">示例 2 :</strong></p>
2225

23-
<pre><strong>输入:</strong> [4,1,2,1,2]
24-
<strong>输出:</strong> 4</pre>
26+
<pre>
27+
<strong>输入:</strong>nums = [4,1,2,1,2]
28+
<strong>输出:</strong>4
29+
</pre>
30+
31+
<p><strong class="example">示例 3 :</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>nums = [1]
35+
<strong>输出:</strong>1
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
44+
<li><code>-3 * 10<sup>4</sup> &lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li>
45+
<li>除了某个元素只出现一次以外,其余每个元素均出现两次。</li>
46+
</ul>
47+
</div>
48+
</div>
2549

2650
## 解法
2751

solution/0400-0499/0409.Longest Palindrome/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
<strong>输入:</strong>1
2929
</pre>
3030

31-
<p>&nbsp;</p>
31+
<p><strong>示例 3:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>s = "aaaaaccc"
35+
<strong>输入:</strong>7</pre>
3236

3337
<p><strong>提示:</strong></p>
3438

solution/0700-0799/0791.Custom Sort String/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定两个字符串 <code>order</code> 和 <code>s</code> 。<code>order</code> 的所有单词都是 <strong>唯一</strong> 的,并且以前按照一些自定义的顺序排序。</p>
9+
<p>给定两个字符串 <code>order</code> 和 <code>s</code> 。<code>order</code> 的所有字母都是 <strong>唯一</strong> 的,并且以前按照一些自定义的顺序排序。</p>
1010

1111
<p>对 <code>s</code> 的字符进行置换,使其与排序的&nbsp;<code>order</code>&nbsp;相匹配。更具体地说,如果在&nbsp;<code>order</code>&nbsp;中的字符 <code>x</code> 出现字符 <code>y</code> 之前,那么在排列后的字符串中, <code>x</code>&nbsp;也应该出现在 <code>y</code> 之前。</p>
1212

13-
<p>返回 <em>满足这个性质的 <code>s</code> 的任意排列&nbsp;</em>。</p>
13+
<p>返回 <em>满足这个性质的 <code>s</code> 的任意一种排列&nbsp;</em>。</p>
1414

1515
<p>&nbsp;</p>
1616

solution/0800-0899/0864.Shortest Path to Get All Keys/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ul>
1212
<li><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'.'</span></span></font></font> 代表一个空房间</li>
13-
<li><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'#'</span></span></font></font> 代表一堵</li>
13+
<li><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'#'</span></span></font></font> 代表一堵墙</li>
1414
<li><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'@'</span></span></font></font>&nbsp;是起点</li>
1515
<li>小写字母代表钥匙</li>
1616
<li>大写字母代表锁</li>

solution/0800-0899/0890.Find and Replace Pattern/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,19 @@ class Solution {
102102
public:
103103
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
104104
vector<string> ans;
105-
for (auto& word : words)
106-
if (match(word, pattern))
107-
ans.push_back(word);
105+
auto match = [](string& s, string& t) {
106+
int m1[128] = {0};
107+
int m2[128] = {0};
108+
for (int i = 0; i < s.size(); ++i) {
109+
if (m1[s[i]] != m2[t[i]]) return 0;
110+
m1[s[i]] = i + 1;
111+
m2[t[i]] = i + 1;
112+
}
113+
return 1;
114+
};
115+
for (auto& word : words) if (match(word, pattern)) ans.emplace_back(word);
108116
return ans;
109117
}
110-
111-
bool match(string s, string t) {
112-
vector<int> m1(128);
113-
vector<int> m2(128);
114-
for (int i = 0; i < s.size(); ++i) {
115-
if (m1[s[i]] != m2[t[i]]) return 0;
116-
m1[s[i]] = i + 1;
117-
m2[t[i]] = i + 1;
118-
}
119-
return 1;
120-
}
121118
};
122119
```
123120

solution/0800-0899/0890.Find and Replace Pattern/README_EN.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,19 @@ class Solution {
9595
public:
9696
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
9797
vector<string> ans;
98-
for (auto& word : words)
99-
if (match(word, pattern))
100-
ans.push_back(word);
98+
auto match = [](string& s, string& t) {
99+
int m1[128] = {0};
100+
int m2[128] = {0};
101+
for (int i = 0; i < s.size(); ++i) {
102+
if (m1[s[i]] != m2[t[i]]) return 0;
103+
m1[s[i]] = i + 1;
104+
m2[t[i]] = i + 1;
105+
}
106+
return 1;
107+
};
108+
for (auto& word : words) if (match(word, pattern)) ans.emplace_back(word);
101109
return ans;
102110
}
103-
104-
bool match(string s, string t) {
105-
vector<int> m1(128);
106-
vector<int> m2(128);
107-
for (int i = 0; i < s.size(); ++i) {
108-
if (m1[s[i]] != m2[t[i]]) return 0;
109-
m1[s[i]] = i + 1;
110-
m2[t[i]] = i + 1;
111-
}
112-
return 1;
113-
}
114111
};
115112
```
116113

solution/0800-0899/0890.Find and Replace Pattern/Solution.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ class Solution {
22
public:
33
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
44
vector<string> ans;
5-
for (auto& word : words)
6-
if (match(word, pattern))
7-
ans.push_back(word);
5+
auto match = [](string& s, string& t) {
6+
int m1[128] = {0};
7+
int m2[128] = {0};
8+
for (int i = 0; i < s.size(); ++i) {
9+
if (m1[s[i]] != m2[t[i]]) return 0;
10+
m1[s[i]] = i + 1;
11+
m2[t[i]] = i + 1;
12+
}
13+
return 1;
14+
};
15+
for (auto& word : words) if (match(word, pattern)) ans.emplace_back(word);
816
return ans;
917
}
10-
11-
bool match(string s, string t) {
12-
vector<int> m1(128);
13-
vector<int> m2(128);
14-
for (int i = 0; i < s.size(); ++i) {
15-
if (m1[s[i]] != m2[t[i]]) return 0;
16-
m1[s[i]] = i + 1;
17-
m2[t[i]] = i + 1;
18-
}
19-
return 1;
20-
}
2118
};

solution/0800-0899/0891.Sum of Subsequence Widths/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
**方法一:排序 + 枚举贡献**
47+
**方法一:排序 + 枚举元素计算贡献**
4848

4949
题目求解的是数组 `nums` 中所有子序列中最大值与最小值差值之和,注意到“子序列”,并且涉及到“最大值”与“最小值”,我们考虑先对数组 `nums` 进行排序。
5050

@@ -66,7 +66,7 @@ $$
6666
\end{aligned}
6767
$$
6868

69-
我们将上述式子展开,可以得到:
69+
我们将上式展开,可以得到:
7070

7171
$$
7272
\begin{aligned}
@@ -82,6 +82,14 @@ $$
8282
\end{aligned}
8383
$$
8484

85+
即:
86+
87+
$$
88+
\begin{aligned}
89+
\sum_{i=0}^{n-1} (nums[i] - nums[n-i-1]) \times 2^i
90+
\end{aligned}
91+
$$
92+
8593
因此我们只需要对数组 `nums` 进行排序,然后计算上述的贡献即可。注意答案的取模操作。
8694

8795
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。

solution/1400-1499/1491.Average Salary Excluding the Minimum and Maximum Salary/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ double average(int* salary, int salarySize) {
194194
sum += salary[i];
195195
ma = max(ma, salary[i]);
196196
mi = min(mi, salary[i]);
197-
}
197+
}
198198
return (sum - mi - ma) * 1.0 / (salarySize - 2);
199199
}
200200
```

solution/1400-1499/1491.Average Salary Excluding the Minimum and Maximum Salary/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ double average(int* salary, int salarySize) {
165165
sum += salary[i];
166166
ma = max(ma, salary[i]);
167167
mi = min(mi, salary[i]);
168-
}
168+
}
169169
return (sum - mi - ma) * 1.0 / (salarySize - 2);
170170
}
171171
```

0 commit comments

Comments
 (0)