...大约 1 分钟
go 语言相关书籍
...小于 1 分钟
接雨水 (trap)
https://leetcode.cn/problems/trapping-rain-water
解法思路
Golang
package main
import (
"fmt"
)
func main() {
nums := []int{
0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1,
}
results := trap(nums)
fmt.Println("results", results)
}
// 暴力求解
/*
*/
func trap(height []int) int {
return 0
}
...小于 1 分钟
三数之和 (threeSum)
https://leetcode.cn/problems/3sum
解法思路
Golang
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{
-1, 0, 1, 2, -1, -4,
}
results := threeSum(nums)
fmt.Println("results", results)
}
// 暴力求解
/*
*/
func threeSum(nums []int) [][]int {
ans := [][]int{}
sort.Ints(nums)
n := len(nums)
// 枚举a
for first := range nums {
if first > 0 && nums[first] == nums[first-1] {
continue
}
// c 对应的指针初始指向数组的最右端
third := n - 1
target := -1 * nums[first]
// 枚举B
for second := first + 1; second < n; second++ {
// 需要和上一次枚举的数不同threeSum
if second > first+1 && nums[second] == nums[second-1] {
continue
}
// 需要保证 b 的指针在 c 的指针的左侧
for second < third && nums[second]+nums[third] > target {
third--
}
// 如果指针重合,随着 b 后续的增加
// 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if second == third {
break
}
if nums[second]+nums[third] == target {
ans = append(ans, []int{nums[first], nums[second], nums[third]})
}
}
}
return ans
}
...小于 1 分钟
盛最多水的容器 (maxArea)
https://leetcode.cn/problems/container-with-most-water
解法思路
Golang
package main
import (
"fmt"
)
func main() {
nums := []int{
1, 8, 6, 2, 5, 4, 8, 3, 7,
}
results := maxArea(nums)
fmt.Println("results", results)
}
// 暴力求解
/*
计算最大容积
容积的公式为 底*高
底 = 当前下标和未来下标的差
高 = 二者之间最大值
*/
func maxArea(height []int) int {
res := 0 // 最大面积
l := 0 // 最左边
r := len(height) - 1 // 最右边
for l < r {
y := 0
x := r - l
if height[l] < height[r] {
y = height[l]
l++
} else {
y = height[r]
r--
}
are := y * x
if are > res {
res = are
}
}
return res
}
...小于 1 分钟
移动零 (moveZeroes)
https://leetcode.cn/problems/move-zeroes
解法思路
Golang
package main
import (
"fmt"
)
func main() {
nums := []int{1, 2, 0, 3, 4, 0, 1, 0, 5, 0}
fmt.Println("nums", nums)
moveZeroes(nums)
fmt.Println("nums", nums)
nums2 := []int{0, 3, 4, 0, 1, 0, 5, 0}
fmt.Println("nums2", nums2)
moveZeroes2(nums2)
fmt.Println("nums2", nums2)
}
// 暴力求解
/*
标记非 0 的值,然后进行替换
*/
func moveZeroes(nums []int) {
notZero := []int{}
for _, item := range nums {
if item != 0 {
notZero = append(notZero, item)
}
}
for idx := range nums {
if idx <= len(notZero)-1 {
nums[idx] = notZero[idx]
} else {
nums[idx] = 0
}
}
}
// 非0值移动
func moveZeroes2(nums []int) {
j := 0
for _, v := range nums {
if v != 0 {
nums[j] = v
j++
}
}
// 将其余值设为0
for i := j; i < len(nums); i++ {
nums[i] = 0
}
}
...小于 1 分钟
最长连续序列 (longestConsecutive)
https://leetcode.cn/problems/longest-consecutive-sequence
解法思路
用 target 挨个 减去 nums 中的项,然后得出结果,如果结果 nums 中存在,则记录下标
JavaScript
...小于 1 分钟
CommonJS 和 ES6 Modules
https://zhuanlan.zhihu.com/p/27644026
要学好 webpack,必须熟悉 JS 模块规范 \
CommonJS 规范:
function isNumber(n) {
return typeof n === 'number';
}
module.exports = {
sum: function (a, b) {
if (isNumber(a) && isNumber(b)) {
return a + b;
} else {
return NaN;
}
},
};
...小于 1 分钟
url 解析
parse 解析
url.parse("地址","是否解析参数 [true/false]","解析没有协议的路径 [true/false]")
format 格式化
url.format("地址对象")
resolve 溶解
url.resolve("地址","目录")
...小于 1 分钟
用 fnm 来管理 node 版本
curl -fsSL https://fnm.vercel.app/install | bash
# ~/.zshenv ~/.bashrc
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node/
eval "$(fnm env --use-on-cd)"
...小于 1 分钟