solutions

package
v0.0.0-...-ac1e4b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch(nums []int, left, right, target int) int

func Calculate

func Calculate(s string) int

Calculate 百度二面

func ClimbStairs

func ClimbStairs(n int) int

ClimbStairs 先把所有值算出来,直接返回

func ClimbStairs2

func ClimbStairs2(n int) int

ClimbStairs2 滚动数组 0 0 1 1 2 p q r

func DoubleNumber

func DoubleNumber(nums []int) []int

DoubleNumber map记录出现次数,时间复杂度 O(log n)

func Generate

func Generate(numRows int) [][]int

Generate 杨辉三角

func GroupAnagrams

func GroupAnagrams(strs []string) [][]string

GroupAnagrams https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked 字母异位词分组 给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。

示例 1:

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"] 输出: [["bat"],["nat","tan"],["ate","eat","tea"]] 示例 2:

输入: strs = [""] 输出: [[""]] 示例 3:

输入: strs = ["a"] 输出: [["a"]]

提示:

1 <= strs.length <= 104 0 <= strs[i].length <= 100 strs[i] 仅包含小写字母 相同字母排列得到的单词,放到一个数组中,通过排序实现

func LongestConsecutive

func LongestConsecutive(nums []int) int

LongestConsecutive map记录数字是否存在,存在就一直用map往后判断,记录最大长度

func MajorityElement

func MajorityElement(nums []int) int

MajorityElement hashtable 记录出现次数

func MajorityElement2

func MajorityElement2(nums []int) int

MajorityElement2 如果将数组 nums 中的所有元素按照单调递增或单调递减的顺序排序, 那么下标为 2/n 的元素一定是众数

func MajorityElement3

func MajorityElement3(nums []int) int

MajorityElement3 Boyer-Moore 投票算法 如果我们把众数记为 +1+1+1,把其他数记为 −1-1−1,将它们全部加起来, 显然和大于 0,从结果本身我们可以看出众数比其他数多。

func MaxProfit

func MaxProfit(prices []int) int

MaxProfit 暴力,超时。。

func MaxProfit2

func MaxProfit2(prices []int) int

MaxProfit2 我们来假设自己来购买股票。随着时间的推移,每天我们都可以选择出售股票与否。那么,假设在第 i 天,如果我们要在今天卖股票,那么我们能赚多少钱呢? 显然,如果我们真的在买卖股票,我们肯定会想:如果我是在历史最低点买的股票就好了!太好了,在题目中,我们只要用一个变量记录一个历史 最低价格 minprice,我们就可以假设自己的股票是在那天买的。那么我们在第 i 天卖出股票能得到的利润就是 prices[i] - minprice。 因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:如果我是在历史最低点买进的,那么我今天卖出能赚多少钱? 当考虑完所有天数之时,我们就得到了最好的答案。

func NumberPositionInSortedArray

func NumberPositionInSortedArray(nums []int, target int) int

NumberPositionInSortedArray 遍历整个有序数组,O(N)

func NumberPositionInSortedArray2

func NumberPositionInSortedArray2(nums []int, target int) int

NumberPositionInSortedArray2 二分查找,找到后左右移动,找到所有等于target的值计算次数 时间复杂度: O(log n + k), k是等于target的数字个数

func NumberPositionInSortedArray3

func NumberPositionInSortedArray3(nums []int, target int) int

NumberPositionInSortedArray3 二分查找,找到左右边界 时间复杂度: O(log n)

func NumberPositionInSortedArray4

func NumberPositionInSortedArray4(nums []int, target int) int

NumberPositionInSortedArray4 标准库

func SingleNumber

func SingleNumber(nums []int) int

SingleNumber 两次循环暴力查找

func SingleNumber2

func SingleNumber2(nums []int) int

SingleNumber2 由于异或运算满足交换律和结合律,所以遍历过程中相同的元素都会异或成0, 而成对出现的元素异或结果也为0,因此最终剩下的就是只出现一次的数字

Types

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

type TreeNode

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL