Skip to content

Commit a45992e

Browse files
halfrostdezhiy
authored andcommitted
Update README
1 parent 8ff3546 commit a45992e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2885
-2174
lines changed

README.md

Lines changed: 1198 additions & 1190 deletions
Large diffs are not rendered by default.

website/content/ChapterFour/0001~0099/0013.Roman-to-Integer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,5 @@ func romanToInt(s string) int {
135135
----------------------------------------------
136136
<div style="display: flex;justify-content: space-between;align-items: center;">
137137
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0012.Integer-to-Roman/">⬅️上一页</a></p>
138-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0015.3Sum/">下一页➡️</a></p>
138+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0014.Longest-Common-Prefix/">下一页➡️</a></p>
139139
</div>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
2+
3+
## 题目
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string "".
8+
9+
**Example 1**:
10+
11+
Input: strs = ["flower","flow","flight"]
12+
Output: "fl"
13+
14+
**Example 2**:
15+
16+
Input: strs = ["dog","racecar","car"]
17+
Output: ""
18+
Explanation: There is no common prefix among the input strings.
19+
20+
**Constraints:**
21+
22+
- 1 <= strs.length <= 200
23+
- 0 <= strs[i].length <= 200
24+
- strs[i] consists of only lower-case English letters.
25+
26+
## 题目大意
27+
28+
编写一个函数来查找字符串数组中的最长公共前缀。
29+
30+
如果不存在公共前缀,返回空字符串 ""。
31+
32+
## 解题思路
33+
34+
- 对 strs 按照字符串长度进行升序排序,求出 strs 中长度最小字符串的长度 minLen
35+
- 逐个比较长度最小字符串与其它字符串中的字符,如果不相等就返回 commonPrefix,否则就把该字符加入 commonPrefix
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
import "sort"
44+
45+
func longestCommonPrefix(strs []string) string {
46+
sort.Slice(strs, func(i, j int) bool {
47+
return len(strs[i]) <= len(strs[j])
48+
})
49+
minLen := len(strs[0])
50+
if minLen == 0 {
51+
return ""
52+
}
53+
var commonPrefix []byte
54+
for i := 0; i < minLen; i++ {
55+
for j := 1; j < len(strs); j++ {
56+
if strs[j][i] != strs[0][i] {
57+
return string(commonPrefix)
58+
}
59+
}
60+
commonPrefix = append(commonPrefix, strs[0][i])
61+
}
62+
return string(commonPrefix)
63+
}
64+
```
65+
66+
67+
----------------------------------------------
68+
<div style="display: flex;justify-content: space-between;align-items: center;">
69+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0013.Roman-to-Integer/">⬅️上一页</a></p>
70+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0015.3Sum/">下一页➡️</a></p>
71+
</div>

website/content/ChapterFour/0001~0099/0015.3Sum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,6 @@ func threeSum1(nums []int) [][]int {
120120

121121
----------------------------------------------
122122
<div style="display: flex;justify-content: space-between;align-items: center;">
123-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0013.Roman-to-Integer/">⬅️上一页</a></p>
123+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0014.Longest-Common-Prefix/">⬅️上一页</a></p>
124124
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0016.3Sum-Closest/">下一页➡️</a></p>
125125
</div>

website/content/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ func maxProduct318(words []string) int {
8080
----------------------------------------------
8181
<div style="display: flex;justify-content: space-between;align-items: center;">
8282
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self/">⬅️上一页</a></p>
83-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0322.Coin-Change/">下一页➡️</a></p>
83+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0319.Bulb-Switcher/">下一页➡️</a></p>
8484
</div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)
2+
3+
4+
## 题目
5+
6+
There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.
7+
8+
On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
9+
10+
Return the number of bulbs that are on after n rounds.
11+
12+
**Example 1:**
13+
14+
Input: n = 3
15+
Output: 1
16+
Explanation: At first, the three bulbs are [off, off, off].
17+
After the first round, the three bulbs are [on, on, on].
18+
After the second round, the three bulbs are [on, off, on].
19+
After the third round, the three bulbs are [on, off, off].
20+
So you should return 1 because there is only one bulb is on.
21+
22+
**Example 2:**
23+
24+
Input: n = 0
25+
Output: 0
26+
27+
**Example 3:**
28+
29+
Input: n = 1
30+
Output: 1
31+
32+
## 题目大意
33+
34+
初始时有 n 个灯泡处于关闭状态。第一轮,你将会打开所有灯泡。接下来的第二轮,你将会每两个灯泡关闭一个。
35+
36+
第三轮,你每三个灯泡就切换一个灯泡的开关(即,打开变关闭,关闭变打开)。第 i 轮,你每 i 个灯泡就切换一个灯泡的开关。直到第 n 轮,你只需要切换最后一个灯泡的开关。
37+
38+
找出并返回 n 轮后有多少个亮着的灯泡。
39+
40+
## 解题思路
41+
42+
- 计算 1 到 n 中有奇数个约数的个数
43+
- 1 到 n 中的某个数 x 有奇数个约数,也即 x 是完全平方数
44+
- 计算 1 到 n 中完全平方数的个数 sqrt(n)
45+
46+
## 代码
47+
48+
```go
49+
50+
package leetcode
51+
52+
import "math"
53+
54+
func bulbSwitch(n int) int {
55+
return int(math.Sqrt(float64(n)))
56+
}
57+
58+
```
59+
60+
61+
----------------------------------------------
62+
<div style="display: flex;justify-content: space-between;align-items: center;">
63+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths/">⬅️上一页</a></p>
64+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0322.Coin-Change/">下一页➡️</a></p>
65+
</div>

website/content/ChapterFour/0300~0399/0322.Coin-Change.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ func coinChange(coins []int, amount int) int {
6363

6464
----------------------------------------------
6565
<div style="display: flex;justify-content: space-between;align-items: center;">
66-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths/">⬅️上一页</a></p>
66+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0319.Bulb-Switcher/">⬅️上一页</a></p>
6767
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0324.Wiggle-Sort-II/">下一页➡️</a></p>
6868
</div>

website/content/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ func (p *pq) Pop() interface{} {
138138
----------------------------------------------
139139
<div style="display: flex;justify-content: space-between;align-items: center;">
140140
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0377.Combination-Sum-IV/">⬅️上一页</a></p>
141-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0385.Mini-Parser/">下一页➡️</a></p>
141+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0384.Shuffle-an-Array/">下一页➡️</a></p>
142142
</div>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [384.Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)
2+
3+
## 题目
4+
5+
Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
6+
7+
Implement the Solution class:
8+
9+
- Solution(int[] nums) Initializes the object with the integer array nums.
10+
- int[] reset() Resets the array to its original configuration and returns it.
11+
- int[] shuffle() Returns a random shuffling of the array.
12+
13+
**Example 1**:
14+
15+
Input
16+
["Solution", "shuffle", "reset", "shuffle"]
17+
[[[1, 2, 3]], [], [], []]
18+
Output
19+
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
20+
21+
Explanation
22+
Solution solution = new Solution([1, 2, 3]);
23+
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
24+
// Any permutation of [1,2,3] must be equally likely to be returned.
25+
// Example: return [3, 1, 2]
26+
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
27+
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
28+
29+
**Constraints:**
30+
31+
- 1 <= nums.length <= 200
32+
- -1000000 <= nums[i] <= 1000000
33+
- All the elements of nums are unique.
34+
- At most 5 * 10000 calls in total will be made to reset and shuffle.
35+
36+
## 题目大意
37+
38+
给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。
39+
40+
实现 Solution class:
41+
42+
- Solution(int[] nums) 使用整数数组 nums 初始化对象
43+
- int[] reset() 重设数组到它的初始状态并返回
44+
- int[] shuffle() 返回数组随机打乱后的结果
45+
46+
## 解题思路
47+
48+
- 使用 rand.Shuffle 进行数组随机打乱
49+
50+
## 代码
51+
52+
```go
53+
54+
package leetcode
55+
56+
import "math/rand"
57+
58+
type Solution struct {
59+
nums []int
60+
}
61+
62+
func Constructor(nums []int) Solution {
63+
return Solution{
64+
nums: nums,
65+
}
66+
}
67+
68+
/** Resets the array to its original configuration and return it. */
69+
func (this *Solution) Reset() []int {
70+
return this.nums
71+
}
72+
73+
/** Returns a random shuffling of the array. */
74+
func (this *Solution) Shuffle() []int {
75+
arr := make([]int, len(this.nums))
76+
copy(arr, this.nums)
77+
rand.Shuffle(len(arr), func(i, j int) {
78+
arr[i], arr[j] = arr[j], arr[i]
79+
})
80+
return arr
81+
}
82+
```
83+
84+
85+
----------------------------------------------
86+
<div style="display: flex;justify-content: space-between;align-items: center;">
87+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix/">⬅️上一页</a></p>
88+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0385.Mini-Parser/">下一页➡️</a></p>
89+
</div>

website/content/ChapterFour/0300~0399/0385.Mini-Parser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ func deserialize(s string) *NestedInteger {
177177

178178
----------------------------------------------
179179
<div style="display: flex;justify-content: space-between;align-items: center;">
180-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix/">⬅️上一页</a></p>
180+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0384.Shuffle-an-Array/">⬅️上一页</a></p>
181181
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0386.Lexicographical-Numbers/">下一页➡️</a></p>
182182
</div>

0 commit comments

Comments
 (0)