随笔分类 - 

LeetCode&牛客网
摘要:15. 三数之和 - 力扣(LeetCode) 思路 双指针法 1. 对输入数组进行校验,是否符合输入要求。 2. 对数组进行排序。 检查排序后的第一个元素,如果第一个元素大于零,则它和任何其他两个元素相加都大于零。返回空值。 3. 首先对数组进行遍历,确定第一个元素V1。 4. 对第一个元素之后的 阅读全文
posted @ 2022-05-21 23:53 SoutherLea 阅读(17) 评论(0) 推荐(0) 编辑
摘要:11. 盛最多水的容器 - 力扣(LeetCode) 思路1 双循环(超时了) 1. 确定计算公式 res = min(height[i], height[j]) * (j - i) 2.使用双循环计算出所有的值,保留最大值。 func maxArea(height []int) int { res 阅读全文
posted @ 2022-05-21 23:26 SoutherLea 阅读(23) 评论(0) 推荐(0) 编辑
摘要:274. H 指数 - 力扣(LeetCode) 思路 func hIndex(citations []int) int { h := 0 sort.Ints(citations) for i := len(citations) - 1; i >= 0 && citations[i] > h; i- 阅读全文
posted @ 2022-05-10 04:51 SoutherLea 阅读(19) 评论(0) 推荐(0) 编辑
摘要:498. 对角线遍历 - 力扣(LeetCode) 思路 找规律 1. 设x,y为数组下标,x+y为偶数时,向右上遍历。x+y为奇数时,向左下遍历。 2. 在向左下遍历时 先考虑边界1 当前在最后一行,则向右移动一格 y+1;再考虑边界 2 当前在第一列,向下移动一格 x+1; 其他情况 x+1,y 阅读全文
posted @ 2022-05-10 04:30 SoutherLea 阅读(23) 评论(0) 推荐(0) 编辑
摘要:79. 单词搜索 - 力扣(LeetCode) 思路 还是回溯 func exist(board [][]byte, word string) bool { if len(board) < 1 || len(board[0]) > 6 || len(word) > 15 || len(word) < 阅读全文
posted @ 2022-05-10 02:34 SoutherLea 阅读(11) 评论(0) 推荐(0) 编辑
摘要:494. 目标和 - 力扣(LeetCode) 思路 依然是回溯法 func findTargetSumWays(nums []int, target int) int { res := 0 if len(nums) == 0 { return 0 } var backTracking func(n 阅读全文
posted @ 2022-05-10 00:29 SoutherLea 阅读(13) 评论(0) 推荐(0) 编辑
摘要:17. 电话号码的字母组合 - 力扣(LeetCode) 思路 回溯法 func letterCombinations(digits string) []string { if len(digits) == 0 || len(digits) > 4 { return nil } strMap := 阅读全文
posted @ 2022-05-09 22:54 SoutherLea 阅读(26) 评论(0) 推荐(0) 编辑
摘要:22. 括号生成 - 力扣(LeetCode) 思路 深度优先遍历 func generateParenthesis(n int) []string { res := make([]string, 0) var dfs func(temp string, leftCount int, rightCo 阅读全文
posted @ 2022-05-09 21:42 SoutherLea 阅读(13) 评论(0) 推荐(0) 编辑
摘要:39. 组合总和 - 力扣(LeetCode) 思路 回溯 还是以递归全排列+剪枝来搞,套用回溯模板。 var res [][]int func combinationSum(candidates []int, target int) [][]int { res = make([][]int, 0) 阅读全文
posted @ 2022-05-09 21:04 SoutherLea 阅读(24) 评论(0) 推荐(0) 编辑
摘要:46. 全排列 - 力扣(LeetCode) 思路 回溯法 var res [][]int func permute(nums []int) [][]int { res = make([][]int, 0) backTracking([]int{}, nums, len(nums)) return 阅读全文
posted @ 2022-05-09 20:27 SoutherLea 阅读(21) 评论(0) 推荐(0) 编辑
摘要:78. 子集 - 力扣(LeetCode) 思路 回溯法 使用回溯模板 1. 无返回值的递归函数 2.通常命名为backtracking 3. 参数依据题意来决定 4. if 终止条件{ 收集结果 return } for (集合内元素集){ 处理节点(将节点存到结果数组中) 递归函数 进行回溯操作 阅读全文
posted @ 2022-05-09 19:32 SoutherLea 阅读(12) 评论(0) 推荐(0) 编辑
摘要:3. 无重复字符的最长子串 - 力扣(LeetCode) (leetcode-cn.com) 思路 1 暴力穷举 1. 使用双指针遍历全部字符串,使用map存储已遍历的字符。 2. 在遍历过程中,如果当前index的字符已在map中存在,则此map长度为子串长度。 3. 保留返回最大的子串长度。 f 阅读全文
posted @ 2022-05-05 20:32 SoutherLea 阅读(22) 评论(0) 推荐(0) 编辑
摘要:101. 对称二叉树 - 力扣(LeetCode) (leetcode-cn.com) 思路 ——>递归 1. 首先确定什么是对称二叉树,即树的左右子树是镜像的。因此我们要分别对左右子树进行递归。 2. 递归终止判断条件:如果左右子树的节点都为空,则返回true;如果其中一个为空,另一个不为空,则认 阅读全文
posted @ 2022-04-30 18:38 SoutherLea 阅读(16) 评论(0) 推荐(0) 编辑
摘要:617. 合并二叉树 - 力扣(LeetCode) (leetcode-cn.com) 思路 深度优先搜索——>递归 1. 从根节点开始,遍历每一个子节点 2. 如果其中一个树的节点为空,将该节点置位另一个树的节点。 3. 如果两个树的节点都不为空,则将val相加。 4. 对当前节点的左右节点都进行 阅读全文
posted @ 2022-04-30 18:02 SoutherLea 阅读(14) 评论(0) 推荐(0) 编辑
摘要:226. 翻转二叉树 - 力扣(LeetCode) (leetcode-cn.com) 思路 递归: 1. 从根节点开始,递归遍历整个树。 2. 对根节点的左节点进行翻转,对根节点的右节点进行翻转。 3. 如果根节点左右节点已经翻转完成,我们只需要互换此根节点的左右节点即可。 /** * Defin 阅读全文
posted @ 2022-04-27 00:53 SoutherLea 阅读(13) 评论(0) 推荐(0) 编辑
摘要:338. 比特位计数 - 力扣(LeetCode) (leetcode-cn.com) 思路 1 逃课法: 1. 使用语言的内置方法 bits.OnesCount func countBits(n int) (result []int) { for i := 0; i <= n; i++ { res 阅读全文
posted @ 2022-04-26 17:59 SoutherLea 阅读(14) 评论(0) 推荐(0) 编辑
摘要:448. 找到所有数组中消失的数字 - 力扣(LeetCode) (leetcode-cn.com) 思路: 1. 由题意可知数组长度n,元素取值范围1~n. 2. 我们可以对数组进行遍历,根据数组元素的值,将它和index=值-1的位置的元素交换数值。如果该位置的元素和当前元素值相等, 则不进行交 阅读全文
posted @ 2022-04-26 17:37 SoutherLea 阅读(15) 评论(0) 推荐(0) 编辑
摘要:136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com) 思路 使用位运算^: 异或运算性质如下 1^1=0 0^0=0 1^0=1 0^1=1 所以我们只需要把数组中的元素遍历异或一遍,剩下的就是那个单独的数字 func singleNumber(nums [] 阅读全文
posted @ 2022-04-26 10:27 SoutherLea 阅读(15) 评论(0) 推荐(0) 编辑
摘要:169. 多数元素 - 力扣(LeetCode) (leetcode-cn.com) 思路 1: 1. 先对所有元素进行排序 2. 返回位于数组最中间的元素 func majorityElement(nums []int) int { lenth:=len(nums) sort.Ints(nums) 阅读全文
posted @ 2022-04-26 02:34 SoutherLea 阅读(12) 评论(0) 推荐(0) 编辑
摘要:461. 汉明距离 - 力扣(LeetCode) (leetcode-cn.com) 思路 1 使用内置方法: func hammingDistance(x int, y int) int { return bits.OnesCount(uint(x^y)) } 思路 2 自己实现功能 1. x^y 阅读全文
posted @ 2022-04-26 02:24 SoutherLea 阅读(11) 评论(0) 推荐(0) 编辑