01 2023 档案
摘要:用go写代码现在还算是比较快了 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func isSymmetric
阅读全文
摘要:dfs 中序遍历即可 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ var num, ans int var
阅读全文
摘要:dfs 取左右子树第二大的值进行比较 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func findSeco
阅读全文
摘要:简单题,重拳出击 func prefixCount(words []string, pref string) int { validCnt := 0 for _, w := range words { notValid := false if len(w) < len(pref) { continu
阅读全文
摘要:正向双指针 有点麻烦,但是能通过,先提交一下,待我学习一下其他的解法再来提交 这个里面不用对opNum进行计数,可以利用left和right的位置计算出来左右两边的长度,可以省略一些,这里我就不重新写了 func minOperations(nums []int, x int) int { if n
阅读全文
摘要:对Go的多维切片的初始化语法不是特别熟悉 还有一种 [[算法-前缀和]] ,但是是二维数组的前缀和,没有深究 func imageSmoother(img [][]int) [][]int { ret := make([][]int, len(img)) for i, v := range img
阅读全文
摘要:657. 机器人能否返回原点 - 力扣(Leetcode) 刚开始用了个 map ,比较复杂,后来看了答案,按照这种简单的方式,并且做了 len(moves) % 2 != 0 的判断 func judgeCircle(moves string) bool { x, y := 0, 0 if len
阅读全文
摘要:653. 两数之和 IV - 输入二叉搜索树 - 力扣(Leetcode) 用了迭代进行遍历二叉树,因为二叉搜索树的中序遍历是有序的,所以肯定左边大于右边,然后就可以用一个map来存放之前的数值, /** * Definition for a binary tree node. * type Tre
阅读全文
摘要:645. 错误的集合 - 力扣(Leetcode) 又用了哈希表,又用了数学计算,看题解有个位运算看不太懂 func findErrorNums(nums []int) []int { m := make(map[int]struct{}, len(nums)) pSum := (1+len(num
阅读全文
摘要:643. 子数组最大平均数 I - 力扣(Leetcode) 滑动窗口,判断好边界条件即可 func findMaxAverage(nums []int, k int) float64 { begin, end := 0, k-1 if end >= len(nums) { end = len(nu
阅读全文
摘要:637. 二叉树的层平均值 - 力扣(Leetcode) 层次遍历+求平均值,Go中的切片也可以模拟queue的功能 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNod
阅读全文
摘要:628. 三个数的最大乘积 - 力扣(Leetcode) 排序后取最小的两个数和最大的数的乘积与前三大的乘积的最大值,防止最小的两个数是负数,负负得正 取这5个数的过程,其实可以直接一次遍历出来,但是排序可以调用标准库 func maximumProduct(nums []int) int { so
阅读全文
摘要:627. 变更性别 - 力扣(Leetcode) # Write your MySQL query statement below update Salary set sex= if(sex='m', 'f', 'm');
阅读全文
摘要:620. 有趣的电影 - 力扣(Leetcode) # Write your MySQL query statement below select * from cinema where description <> "boring" and id%2 = 1 order by rating des
阅读全文
摘要:617. 合并二叉树 - 力扣(Leetcode) 递归合并二叉树 easy /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode
阅读全文
摘要:607. 销售员 - 力扣(Leetcode) 这个有点像是写离线查询了 # Write your MySQL query statement below select sp.name from ( select sales_id, name from SalesPerson ) sp left o
阅读全文
摘要:606. 根据二叉树创建字符串 - 力扣(Leetcode) 前序遍历 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * }
阅读全文
摘要:605. 种花问题 - 力扣(Leetcode) 下面是中间有0的情况下,可以种植的个数 1 0 2 0 3 1 4 1 5 2 6 2 7 3 8 3 9 4 两边边界问题,左边我使用 begin 往后挪了两位后认为是1,右边往右挪了两位后算作1 func canPlaceFlowers(flow
阅读全文
摘要:599. 两个列表的最小索引总和 - 力扣(Leetcode) 刚开始的思路是搞两个map,但是性能比较差,只需要构建一个map然后遍历第二个list即可 [!添加后可以过滤一些肯定不符合条件的] if k1 > indexSum { continue } func findRestaurant(l
阅读全文
摘要:598. 范围求和 II - 力扣(Leetcode) Go语言没有针对int的最小值、最大值以及比较算法,可以有一套,不然每次都需要写这个min函数 func maxCount(m int, n int, ops [][]int) int { if m <= 0 || n <= 0 { retur
阅读全文
摘要:595. 大的国家 - 力扣(Leetcode) 两种方法,一个是or一个是union,union会快一点 # Write your MySQL query statement below # select name, population, area from World # where area
阅读全文
摘要:594. 最长和谐子序列 - 力扣(Leetcode) 双指针,后面那个边界条件让我调了好几分钟 func findLHS(nums []int) int { sort.Ints(nums) left, right, maxLen := 0, 0, 0 for { if nums[right] -
阅读全文
摘要:590. N 叉树的后序遍历 - 力扣(Leetcode) 可以参考 [[leetcode-589. N 叉树的前序遍历]] ,代码差不多 /** * Definition for a Node. * type Node struct { * Val int * Children []*Node *
阅读全文
摘要:589. N 叉树的前序遍历 - 力扣(Leetcode) Go语言的切片操作方便性还不错 /** * Definition for a Node. * type Node struct { * Val int * Children []*Node * } */ func preorder(root
阅读全文
摘要:586. 订单最多的客户 - 力扣(Leetcode) # Write your MySQL query statement below select customer_number from ( select customer_number, count(*) cnt from Orders gr
阅读全文
摘要:584. 寻找用户推荐人 - 力扣(Leetcode) sql 题,还是比较简单的 # Write your MySQL query statement below select name from customer where referee_id <> 2 or referee_id is nu
阅读全文
摘要:575. 分糖果 - 力扣(Leetcode) 信息: 糖果的种类 总个数 吃一半 分析: 种类大于一半,那么只能吃一半 种类小于一半,那么是种类量 考哈希表,有点简单,熟悉Go语法还行 func distributeCandies(candyType []int) int { if len(can
阅读全文