Skip to content

Commit 58dda9d

Browse files
committed
docs: 更新多个题解文件以匹配 leetcoder.net 的格式
1 parent 26e2838 commit 58dda9d

9 files changed

+237
-37
lines changed

en/1-1000/202-happy-number.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# 202. Happy Number - Best Practices of LeetCode Solutions
1+
Original link: [leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/202-happy-number)
2+
3+
# 202. Happy Number - Fucking Good LeetCode Solutions
24
LeetCode link: [202. Happy Number](https://leetcode.com/problems/happy-number), difficulty: **Easy**.
35

46
## LeetCode description of "202. Happy Number"

en/1-1000/28-find-the-index-of-the-first-occurrence-in-a-string.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# 28. Find the Index of the First Occurrence in a String - Best Practices of LeetCode Solutions
1+
Original link: [leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
2+
3+
# 28. Find the Index of the First Occurrence in a String - Fucking Good LeetCode Solutions
24
LeetCode link: [28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string), difficulty: **Easy**.
35

46
## LeetCode description of "28. Find the Index of the First Occurrence in a String"
@@ -100,7 +102,27 @@ var strStr = function (haystack, needle) {
100102
# Welcome to create a PR to complete the code of this language, thanks!
101103
```
102104

103-
## C, Kotlin, Swift, Rust or other languages
105+
## C
106+
```c
107+
// Welcome to create a PR to complete the code of this language, thanks!
108+
```
109+
110+
## Kotlin
111+
```kotlin
112+
// Welcome to create a PR to complete the code of this language, thanks!
113+
```
114+
115+
## Swift
116+
```swift
117+
// Welcome to create a PR to complete the code of this language, thanks!
118+
```
119+
120+
## Rust
121+
```rust
122+
// Welcome to create a PR to complete the code of this language, thanks!
123+
```
124+
125+
## Other languages
104126
```
105127
// Welcome to create a PR to complete the code of this language, thanks!
106128
```

en/1-1000/349-intersection-of-two-arrays.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# 349. Intersection of Two Arrays - Best Practices of LeetCode Solutions
1+
Original link: [leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/349-intersection-of-two-arrays)
2+
3+
# 349. Intersection of Two Arrays - Fucking Good LeetCode Solutions
24
LeetCode link: [349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays), difficulty: **Easy**.
35

46
## LeetCode description of "349. Intersection of Two Arrays"
@@ -26,9 +28,31 @@ Each element in the result must be **unique** and you may return the result in *
2628
2. When traversing the other array, if an element is found to already exist in the `set`, it means that the element belongs to the intersection, and the element should be added to the `results`.
2729
3. The `results` is also of `set` type because duplicate removal is required.
2830

31+
## Steps
32+
33+
1. Convert one of the arrays to a `set`. The elements are unique in a `set`.
34+
35+
```javascript
36+
let num1Set = new Set(nums1)
37+
```
38+
39+
2. When traversing the other array, if an element is found to already exist in the `set`, it means that the element belongs to the intersection, and the element should be added to the `results`.
40+
41+
```javascript
42+
let results = new Set()
43+
44+
for (const num of nums2) {
45+
if (num1Set.has(num)) {
46+
results.add(num)
47+
}
48+
}
49+
50+
return Array.from(results)
51+
```
52+
2953
## Complexity
30-
* Time: `O(N)`.
31-
* Space: `O(N)`.
54+
* Time: `O(n)`.
55+
* Space: `O(n)`.
3256

3357
## Java
3458
```java

en/1-1000/383-ransom-note.md

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# 383. Ransom Note - Best Practices of LeetCode Solutions
1+
# 383. Ransom Note - Fucking Good LeetCode Solutions
2+
23
LeetCode link: [383. Ransom Note](https://leetcode.com/problems/ransom-note),
34
[383. 赎金信](https://leetcode.cn/problems/ransom-note)
45

@@ -256,3 +257,104 @@ for (character in ransomNote) {
256257

257258
return true
258259
```
260+
261+
## 复杂度
262+
* 时间:`O(n)`
263+
* 空间:`O(n)`
264+
265+
## Java
266+
```java
267+
class Solution {
268+
public boolean canConstruct(String ransomNote, String magazine) {
269+
var charToCount = new HashMap<Character, Integer>();
270+
271+
for (var character : magazine.toCharArray()) {
272+
charToCount.put(character, charToCount.getOrDefault(character, 0) + 1);
273+
}
274+
275+
for (var character : ransomNote.toCharArray()) {
276+
charToCount.put(character, charToCount.getOrDefault(character, 0) - 1);
277+
278+
if (charToCount.get(character) < 0) {
279+
return false;
280+
}
281+
}
282+
283+
return true;
284+
}
285+
}
286+
```
287+
288+
## Python
289+
```python
290+
# from collections import defaultdict
291+
292+
class Solution:
293+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
294+
char_to_count = defaultdict(int)
295+
296+
for char in magazine:
297+
char_to_count[char] += 1
298+
299+
for char in ransomNote:
300+
char_to_count[char] -= 1
301+
302+
if char_to_count[char] < 0:
303+
return False
304+
305+
return True
306+
```
307+
308+
## JavaScript
309+
```javascript
310+
var canConstruct = function (ransomNote, magazine) {
311+
const charToCount = new Map()
312+
313+
for (const character of magazine) {
314+
charToCount.set(character, (charToCount.get(character) || 0) + 1)
315+
}
316+
317+
for (const character of ransomNote) {
318+
charToCount.set(character, (charToCount.get(character) || 0) - 1)
319+
320+
if (charToCount.get(character) < 0) {
321+
return false
322+
}
323+
}
324+
325+
return true
326+
};
327+
```
328+
329+
## C#
330+
```c#
331+
public class Solution
332+
{
333+
public bool CanConstruct(string ransomNote, string magazine)
334+
{
335+
var charToCount = new Dictionary<char, int>();
336+
337+
foreach (char character in magazine)
338+
charToCount[character] = charToCount.GetValueOrDefault(character, 0) + 1;
339+
340+
foreach (char character in ransomNote)
341+
{
342+
charToCount[character] = charToCount.GetValueOrDefault(character, 0) - 1;
343+
344+
if (charToCount[character] < 0)
345+
{
346+
return false;
347+
}
348+
}
349+
350+
return true;
351+
}
352+
}
353+
```
354+
355+
## Other languages
356+
```java
357+
// Welcome to create a PR to complete the code of this language, thanks!
358+
```
359+
360+
原文链接:[leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/383-ransom-note)

en/1-1000/454-4sum-ii.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# 454. 4Sum II - Best Practices of LeetCode Solutions
2-
LeetCode link: [454. 4Sum II](https://leetcode.com/problems/problem), difficulty: **Medium**.
1+
原文链接:[leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/454-4sum-ii)
2+
3+
# 454. 4Sum II - Fucking Good LeetCode Solutions
4+
LeetCode link: [454. 4Sum II](https://leetcode.com/problems/4sum-ii), difficulty: **Medium**.
35

46
## LeetCode description of "454. 4Sum II"
57
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:

zh/1-1000/202-happy-number.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
原文链接:[leetcoder.net - 力扣题解最佳实践](https://leetcoder.net/zh/leetcode/202-happy-number)
2+
13
# 202. 快乐数 - 力扣题解最佳实践
24
力扣链接:[202. 快乐数](https://leetcode.cn/problems/happy-number) ,难度:**简单**
35

4-
## 力扣202. 快乐数问题描述
6+
## 力扣"202. 快乐数"问题描述
57
编写一个算法来判断一个数 `n` 是不是快乐数。
68

7-
**快乐数**定义为:
9+
「快乐数定义为:
810

911
- 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
10-
- 然后重复这个过程直到这个数变为 `1`,也可能是 **无限循环** 但始终变不到 `1`
11-
- 如果这个过程 **结果为** `1`,那么这个数就是快乐数。
12-
- 如果 `n`*快乐数* 就返回 `true` ;不是,则返回 `false`
12+
- 然后重复这个过程直到这个数变为 1,也可能是 **无限循环** 但始终变不到 1。
13+
- 如果 **可以变为** 1,那么这个数就是快乐数。
14+
15+
如果 `n` 是快乐数就返回 `true` ;不是,则返回 `false`
1316

1417
### [示例 1]
1518
**输入**: `n = 19`
@@ -18,10 +21,10 @@
1821

1922
**解释**:
2023
```
21-
1**2 + 9**2 = 82
22-
8**2 + 2**2 = 68
23-
6**2 + 8**2 = 100
24-
1**2 + 0**2 + 0**2 = 1
24+
1^2 + 9^2 = 82
25+
8^2 + 2^2 = 68
26+
6^2 + 8^2 = 100
27+
1^2 + 0^2 + 0^2 = 1
2528
```
2629

2730
### [示例 2]
@@ -30,15 +33,15 @@
3033
**输出**: `false`
3134

3235
### [约束]
33-
- `1 <= n <= 2**31 - 1`
36+
- `1 <= n <= 2^31 - 1`
3437

3538
## 思路
36-
1. 递归调用`isHappy(n)`比较方便,每次只需要生成新的`n`作为参数。
37-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
39+
1. 递归调用 `isHappy(n)` 更方便。每次只需要生成新的 `n` 作为参数。
40+
2. 如果 `n` 已经出现过,说明进入了循环,`return false`。可以用 `Set` 保存出现过的 `n`
3841

3942
## 步骤
4043

41-
1. 生成新的`n`作为`isHappy()`的参数。
44+
1. 生成新的 `n` 作为 `isHappy(n)` 的参数。
4245

4346
```javascript
4447
let sum = 0
@@ -52,10 +55,10 @@
5255
return isHappy(sum)
5356
```
5457

55-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
58+
2. 如果 `n` 已经出现过,说明进入了循环,`return false`。可以用 `Set` 保存出现过的 `n`
5659

5760
```javascript
58-
var isHappy = function (n, appearedNums) {
61+
var isHappy = function (n, appearedNums) { // 0
5962
appearedNums ||= new Set() // 1
6063
let sum = 0
6164

@@ -78,8 +81,8 @@
7881
```
7982

8083
## 复杂度
81-
* 时间`O(log N)`
82-
* 空间`O(log N)`
84+
* 时间: `O(log N)`.
85+
* 空间: `O(log N)`.
8386

8487
## Java
8588
```java

zh/1-1000/28-find-the-index-of-the-first-occurrence-in-a-string.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
原文链接:[leetcoder.net - 力扣题解最佳实践](https://leetcoder.net/zh/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
2+
13
# 28. 找出字符串中第一个匹配项的下标 - 力扣题解最佳实践
24
力扣链接:[28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string) ,难度:**简单**
35

4-
## 力扣28. 找出字符串中第一个匹配项的下标问题描述
6+
## 力扣"28. 找出字符串中第一个匹配项的下标"问题描述
57
给你两个字符串 `haystack``needle` ,请你在 `haystack` 字符串中找出 `needle` 字符串的第一个匹配项的下标(下标从 `0` 开始)。如果 `needle` 不是 `haystack` 的一部分,则返回 `-1`
68

79
### [示例 1]

zh/1-1000/383-ransom-note.md

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
# 383. 赎金信 - 力扣题解最佳实践
44

5-
力扣链接:[383. 赎金信](https://leetcode.cn/problems/ransom-note), 难度:**简单**
5+
力扣链接:[383. 赎金信](https://leetcode.cn/problems/ransom-note)难度:**简单**
66

7-
## 力扣383. 赎金信问题描述
7+
## 力扣"383. 赎金信"问题描述
88

99
给你两个字符串:`ransomNote``magazine` ,判断 `ransomNote` 能不能由 `magazine` 里面的字符构成。
1010

@@ -29,15 +29,14 @@
2929
**输出**: `true`
3030
### [约束]
3131

32-
- `1 <= ransomNote.length, magazine.length <= 10^5`
32+
- `1 <= ransomNote.length, magazine.length <= 100000`
3333
- `ransomNote``magazine` 由小写英文字母组成
3434

3535
## 思路
3636

3737
1. 本题等同于求`magazine`是否能包含`ransomNote`中的所有字符。
38-
2. 先对`magazine`进行统计,得出每个字符对应的字数,结果存储在`Map`中。每一次都是一个加一的操作。
39-
3. 下一步做什么?
40-
<details><summary>点击查看答案</summary><p>遍历`ransomNote`,对当前字符对应的数量进行减一操作(反向操作)。如果某个字符的数量小于0,则返回`false`。</p></details>
38+
2. 先对`magazine`进行字符和字数统计,结果存储在`Map`中。
39+
3. 然后,遍历`ransomNote`,并对`Map`中的数据进行反向操作。如果某个字符的字数小于0,则返回`false`
4140

4241
## 步骤
4342

@@ -73,8 +72,8 @@
7372

7473
## 复杂度
7574

76-
- 时间复杂度: `O(N)`.
77-
- 空间复杂度: `O(N)`.
75+
- 时间复杂度: `O(n)`.
76+
- 空间复杂度: `O(n)`.
7877

7978
## Java
8079

@@ -170,8 +169,50 @@ public class Solution
170169
}
171170
```
172171

172+
## C++
173+
174+
```cpp
175+
// Welcome to create a PR to complete the code of this language, thanks!
176+
```
177+
178+
## Go
179+
180+
```go
181+
// Welcome to create a PR to complete the code of this language, thanks!
182+
```
183+
184+
## Ruby
185+
186+
```ruby
187+
# Welcome to create a PR to complete the code of this language, thanks!
188+
```
189+
190+
## C
191+
192+
```c
193+
// Welcome to create a PR to complete the code of this language, thanks!
194+
```
195+
196+
## Kotlin
197+
198+
```kotlin
199+
// Welcome to create a PR to complete the code of this language, thanks!
200+
```
201+
202+
## Swift
203+
204+
```swift
205+
// Welcome to create a PR to complete the code of this language, thanks!
206+
```
207+
208+
## Rust
209+
210+
```rust
211+
// Welcome to create a PR to complete the code of this language, thanks!
212+
```
213+
173214
## Other languages
174215

175-
```java
216+
```
176217
// Welcome to create a PR to complete the code of this language, thanks!
177218
```

0 commit comments

Comments
 (0)