leetcode 376.摆动序列
376. 摆动序列 - 力扣(Leetcode) 12345678910111213141516171819func wiggleMaxLength(nums []int) int { var count, preDiff, curDiff int count = 1 // 初始化计数为1,至少有一个数字是有效的 if len(nums) < 2 { return count // 如果数组长度小于2,直接返回计数值 } for i := 0; i < len(nums)-1; i++ { curDiff = nums[i+1] - nums[i] // 计算当前数字之间的差值 // 根据差值的正负和前一个差值的正负进行判断 // 如果满足摆动序列的条件,更新前一个差值和计数值 if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0...
leetcode 226. 翻转二叉树
226. 翻转二叉树 - 力扣(Leetcode) 1234567891011121314151617181920212223/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ // 定义将二叉树翻转func invertTree(root *TreeNode) *TreeNode { // 递归终止条件 if root == nil { return nil } // 单个任务逻辑 交换root 下的两个节点,然后在严格按照定义递归调用左右节点 root.Right,root.Left = root.Left,root.Right // 将右子树翻转 invertTree(root.Right) // 将左子树翻转 ...
leetcode 144. 二叉树的前序遍历
144. 二叉树的前序遍历 - 力扣(Leetcode) 记得提前判断是否为空,否则会报找不到内存指针的错误 注意:这里和层序遍历不一样,这里不用使用中间变量lens := stack.len() 来遍历每层,虽然增加了每层遍历依然可以通过,但是没有必要。只有在层序遍历的时候才需要记录每层的信息。leetcode 102. 二叉树的层序遍历 12345678910111213141516171819202122232425262728/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func preorderTraversal(root *TreeNode) []int { stack := list.New() res := []int{} if root ==...
leetcode 102. 二叉树的层序遍历
102. 二叉树的层序遍历 - 力扣(Leetcode) 使用slice123456789101112131415161718192021222324252627282930313233343536373839404142/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func levelOrder(root *TreeNode) [][]int { // 层序遍历 使用size 记录每层数组 queue node 队列 res := make([][]int, 0) queue := make([]*TreeNode, 0) if root != nil { queue = append(queue, root) } else { return res ...
leetcode 1845.座位预约管理系统
1845. 座位预约管理系统 - 力扣(Leetcode) 超时版本123456789101112131415161718192021222324252627282930313233type seat struct { seatId int isFree int // 空}type SeatManager struct { seats map[int]*seat isFrees []int // 可预约的使用list 保存一份,记得被占用的时候,删除该座位,空缺则添加}func Constructor(n int) SeatManager { var a = SeatManager{make(map[int]*seat, n), make([]int, n)} for i := 0; i < n; i++ { id := i + 1 a.seats[id] = &seat{id, 1} a.isFrees[i] = id //...
leetcode 455.分发饼干
455. 分发饼干 - 力扣(Leetcode) https://leiqicn.gitee.io/ 欢迎关注我的博客,定时更新golang 刷题笔记 贪心的策略: 贪心算法一般分为如下四步: 将问题分解为若干个子问题找出适合的贪心策略求解每一个子问题的最优解将局部最优解堆叠成全局最优解 排序,遍历饼干,child胃口初始化idx=0 ,res =0 ;如果没有越界并且饼干大于等于胃口,则childIdx ++;res++ 12345678910111213141516171819func findContentChildren(g []int, s []int) int { // g 小孩胃口 s 饼干 从大到小排序 排序,并给胃口初始化赋值 sort.Ints(g) sort.Ints(s) j := 0 res := 0 // 用小饼干 来满足最小的胃口 for i := 0; i < len(s); i++ { // 判断越界 饼干大于胃口 if j...
网站无法访问categories?
在package.json中没有放 下边的hexo-generator-category,导致无法生成对应的文件。 可以显示啦
leetcode 706. 设计哈希映射 Golang
706. 设计哈希映射 - 力扣(Leetcode)使用了go 语言的list.List (双向列表),具体如何使用请看文章:Go语言-list.List 使用结构体(非指针)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// 链地址法var base = 769// base := 769type entry struct { k int v int}type MyHashMap struct { hashMaps []list.List}// hash集合可以使用数组链表;func Constructor() MyHashMap { return MyHashMap{make([]list.List, base)}}// func (this *MyHashMap) hash(key int, value...
leetcode 705. 设计哈希集合
705. 设计哈希集合 - 力扣(Leetcode) 1234567891011121314151617181920212223242526272829303132333435363738394041const base = 769 // 哈希表的长度 质数type MyHashSet struct { data []list.List // 使用链表储存冲突元素}func Constructor() MyHashSet { // 构造函数,返回一个空的哈希集合 return MyHashSet{make([]list.List, base)}}// 哈希函数:对键值取模得到哈希值func (s *MyHashSet) hash(key int) int { return key % base}func (s *MyHashSet) Add(key int) { // 向哈希集合中添加元素 if !s.Contains(key) { //...
leetcode 1396.设计地铁系统
1396. 设计地铁系统 - 力扣(Leetcode) 设计题: 设计数据结构 user startTime int endTime int startStationName string endStatationName string UndergroundSystem userMap map[int]*user pathMap map[string][]int // 存放对应路程的用时,用于计算平均时间 注意点:go语言针对结构体包含指针的,需要在具体实现前初始化。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364type user struct { id int startTime int endTime int startStationName string endStatationName string}type...