跳至主要內容
墨七的书单

墨七的书单

《 行为科学统计 》

豆瓣读书-行为科学统计

这是本极佳的统计学入门书籍,不单讲理论,还会让你明白为什么要这么做,应该怎么做。

《 深入浅出数据分析 》


墨七...大约 1 分钟
接雨水 (trap)

接雨水 (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 分钟leetcodeleetcode
三数之和 (threeSum)

三数之和 (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 分钟leetcodeleetcode
盛最多水的容器 (maxArea)

盛最多水的容器 (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 分钟leetcodeleetcode
移动零 (moveZeroes)

移动零 (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 分钟leetcodeleetcode
CommonJS 和 ES6 Modules

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 解析

url 解析

parse  解析
url.parse("地址","是否解析参数 [true/false]","解析没有协议的路径 [true/false]")

format 格式化
url.format("地址对象")

resolve 溶解
url.resolve("地址","目录")

墨七...小于 1 分钟
2
3
4
5
6