
leetcode刷题
皮卡猴
中科院硕士,喜欢写写画画的理工宅男,信奉爱生活,爱coding的理想生活状态。目前正在准备论文和2022届秋招!
展开
-
1385. 两个数组间的距离值
1385. 两个数组间的距离值1. 问题描述2. 思路暴力3. 代码func findTheDistanceValue(arr1 []int, arr2 []int, d int) int { res := 0 for i := 0; i < len(arr1); i++ { if isDistance(arr1[i], arr2, d) { res++ } } return res}func原创 2022-05-21 19:42:18 · 264 阅读 · 1 评论 -
Leetcode1351. 统计有序矩阵中的负数
Leetcode1351. 统计有序矩阵中的负数1. 问题描述2. 思路2.1 思路1暴力2.2 思路2二分查找3. 代码3.1 思路1代码func countNegatives(grid [][]int) int { res := 0 for i := 0; i < len(grid); i++ { for j := 0; j < len(grid[i]); j++ { if grid[i][j] < 0 {原创 2022-05-20 09:23:44 · 221 阅读 · 0 评论 -
Leetcode 1346. 检查整数及其两倍数是否存在
Leetcode 1346. 检查整数及其两倍数是否存在1. 问题描述2. 思路2.1 思路1暴力, 双层for循环3. 代码func checkIfExist(arr []int) bool { for i := 0; i < len(arr); i++ { for j := 0; j < len(arr); j++ { if i == j { break }原创 2022-05-19 11:03:16 · 272 阅读 · 0 评论 -
Leetcode 1337. 矩阵中战斗力最弱的 K 行
Leetcode 1337. 矩阵中战斗力最弱的 K 行1. 问题描述2. 思路计算每一行士兵的战斗力,存入power对power排序3. 代码type Power struct { Value int Row int}func kWeakestRows(mat [][]int, k int) []int { var res []int var powers []Power for i := 0; i < len(mat); i++原创 2022-05-19 10:04:35 · 213 阅读 · 0 评论 -
Leetcode1283. 使结果不超过阈值的最小除数
Leetcode1283. 使结果不超过阈值的最小除数1. 问题描述2. 思路二分查找,左边界为1,有边界为数组最大值3. 代码func smallestDivisor(nums []int, threshold int) int { left, right := 1, getMax(nums) for left <= right { mid := left + (right - left) / 2 temp := countSum(num原创 2022-05-16 11:14:55 · 363 阅读 · 0 评论 -
Leetcode 1237. 找出给定方程的正整数解
Leetcode 1237. 找出给定方程的正整数解1. 问题描述2. 思路二分查找固定x,二分查找y3. 代码/** * This is the declaration of customFunction API. * @param x int * @param x int * @return Returns f(x, y) for any given positive integers x and y. * Note that f(x,原创 2022-05-15 18:58:05 · 349 阅读 · 0 评论 -
Leetcode 1170. 比较字符串最小字母出现频次
Leetcode 1170. 比较字符串最小字母出现频次1. 问题描述2. 思路2.1 思路1暴力3. 代码3.1 思路1代码func numSmallerByFrequency(queries []string, words []string) []int { var res []int counterQ := getCounter(queries) counterW := getCounter(words) for i := 0; i < len(原创 2022-05-13 16:03:09 · 974 阅读 · 0 评论 -
Leetcode1011. 在D天内送达包裹的能力
Leetcode1011. 在D天内送达包裹的能力1. 问题描述2. 思路二分查找3. 代码func shipWithinDays(weights []int, days int) int { sum := 0 for i := 0; i < len(weights); i++ { sum += weights[i] } max := getMax(weights) left, right := max, sum for l原创 2022-05-12 19:40:08 · 263 阅读 · 0 评论 -
Leetcode888. 公平的糖果交换
Leetcode888. 公平的糖果交换1. 问题描述2. 思路2.1 思路1暴力求解3. 代码func fairCandySwap(aliceSizes []int, bobSizes []int) []int { var res []int aliceSum, bobSum := 0, 0 for i := 0; i < len(aliceSizes); i++ { aliceSum += aliceSizes[i] } fo原创 2022-05-11 15:09:00 · 311 阅读 · 0 评论 -
Leetcode 875. 爱吃香蕉的珂珂
Leetcode 875. 爱吃香蕉的珂珂1. 问题描述思路二分查找代码func minEatingSpeed(piles []int, h int) int { sum := 0 for i := 0; i < len(piles); i++ { sum += piles[i] } left, right := 1, sum for left <= right { mid := left + (right -原创 2022-05-10 10:15:16 · 244 阅读 · 0 评论 -
Leetcode 852. 山脉数组的峰顶索引
Leetcode 852. 山脉数组的峰顶索引1. 问题描述2. 思路3. 代码func peakIndexInMountainArray(arr []int) int { i := 0 for i + 1 < len(arr) && arr[i+1] > arr[i] { i++ } return i}原创 2022-05-09 10:07:55 · 241 阅读 · 0 评论 -
Leetcode 826. 安排工作以达到最大收益
Leetcode 826. 安排工作以达到最大收益1. 问题描述2. 思路3. 代码func maxProfitAssignment(difficulty []int, profit []int, worker []int) int { var res int profitMap := make(map[int]int, 0) length := len(profit) for i := 0; i < length; i++ { _, ok原创 2022-05-08 14:06:21 · 607 阅读 · 0 评论 -
Leetcode825. 适龄的朋友
Leetcode825. 适龄的朋友1. 问题描述2. 思路找朋友不找比自己大的找朋友不找比自己小的多的3. 代码func numFriendRequests(ages []int) int { var res int sort.Ints(ages) for i := 0; i < len(ages); i++ { if ages[i] < 15 { continue } inde原创 2022-05-07 19:42:13 · 448 阅读 · 0 评论 -
Leetcode 754. 到达终点数字
Leetcode 754. 到达终点数字1. 问题描述2. 思路纯粹 数学找规律问题3. 代码class Solution { public int reachNumber(int target) { target = Math.abs(target); int k = 0; while (target > 0) { k++; target = target - k;原创 2022-05-06 11:13:46 · 985 阅读 · 0 评论 -
Leetcode 658. 找到k个最接近的元素
Leetcode 658. 找到k个最接近的元素1. 问题描述2. 思路 按照递增序列,找到x应该插入的位置index。以此为中心,向两侧扩展长度为k的空间,该空间内的元素就是我们想要的元素二分查找,找到满足<= x的元素的最大下标index以index为中心,令i = index, j = index + 1,向两侧扩展,循环k次如果i >= 0,且 j <= len(arr) - 1如果arr[i]更接近x,向左移动i如果arr[j]更接近x,向右移动j原创 2022-05-03 12:54:49 · 242 阅读 · 0 评论 -
Leetcode 653. 两数之和IV - 输入BST
Leetcode 653. 两数之和IV - 输入BST1. 问题描述2. 思路遍历二叉搜索树,将遍历到的结果收集到map中,key为当前节点root.Val判断map[k-root.Val]是否存在,如果存在返回true,不存在返回false3. 代码/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Righ原创 2022-05-02 14:37:34 · 321 阅读 · 0 评论 -
Leetcode 633. 平方数之和
Leetcode 633. 平方数之和1. 问题描述2. 思路逆向双指针,左边界从0 开始,右边界从c\sqrt{c}c 位置开始3. 代码func judgeSquareSum(c int) bool { left, right := 0, int(math.Sqrt(float64(c))) for left <= right { sum := left * left + right * right if sum == c {原创 2022-05-01 11:28:46 · 1447 阅读 · 0 评论 -
LeetCode611. 有效三角形的个数
LeetCode611. 有效三角形的个数1. 问题描述2. 思路三条长度为 a、b、c 的线段,能够组成三角形的充要条件是:a+b>ca + b > ca+b>ca+c>ba + c > ba+c>bb+c>ab + c > ab+c>a2.1 思路一 解决本题的过程,就是一个搜索的过程。一种解决方案是,获取nums数组中所有长度为3的组合,判断能否满足上面三角形构成的条件。 上述解决方案是使用暴力回溯的方式,从长度为3的组合中原创 2022-04-30 16:43:25 · 436 阅读 · 0 评论 -
Leetcode 540. 有序数组中的单一元素
Leetcode 540. 有序数组中的单一元素1. 问题描述2. 思路只看偶数下标对应的元素,单一元素左侧,偶数下标为第一个元素,单一元素右侧,偶数下标为第二个元素二分查找的目标是找到满足 nums[x] !=nums[x+1] 的最小的偶数下标 xx,则下标 xx 处的元素是只出现一次的元素。3. 代码func singleNonDuplicate(nums []int) int { left, right := 0, len(nums)-1 for left < r原创 2022-04-29 11:21:10 · 301 阅读 · 0 评论 -
Leetcode456. 132模式
Leetcode456. 132模式1. 问题描述2. 思路使用单调栈维护"3"(1)单调栈维护的是3,mid维护的是2,枚举的是1(2)维护的值尽可能让它大,枚举的值尽可能让它小基本思想:从后往前遍历,维护一个单调递减的栈,同时使用mid记录所有出栈元素中的最大值(栈中的元素比mid大 这就是一个32模式);那么当我们遍历到i,只要满足发现num[i] < k,说明我们找到了符合条件的i j k.举一个例子:[3,5,0,3,4]【1】枚举到4:栈内元素为【4】 mid = I原创 2022-04-28 11:33:45 · 161 阅读 · 0 评论 -
Leetcode441. 排列硬币
Leetcode441. 排列硬币1. 问题描述2. 思路线性扫描二分查找前n行如果全部有硬币,总数为:n * (n - 1))/ 23. 代码3.1 思路1func arrangeCoins(n int) int { var res int count := 1 for n >= count { n = n - count count++ res++ } return res}3.原创 2022-04-27 21:55:02 · 319 阅读 · 0 评论 -
Leetcode 436. 寻找右区间
Leetcode 436. 寻找右区间1. 问题描述2. 思路遍历intervals数组,对于intervals[i]1.1 查找满足intervals[][0] >= intervals[i][1]的元素对应的索引index1.2 收集index1.1中的查找过程,有多种方式,可以线性查找,也可以进行二分查找。如果使用二分查找,需要将intervals[][0]预先存放到一个新数组中,进行排序,再进行二分查找,由于排序会导致索引发生变化,因此需要另开map来将索引存起来3.原创 2022-04-26 15:55:09 · 233 阅读 · 0 评论 -
Leetcode400. 第N位数字
Leetcode400. 第N位数字1. 问题描述2. 思路找到第n位数字属于的整数num找到第n位数字在num中的索引index解析出num中的第index个数3. 代码func findNthDigit(n int) int { d := 1 for count := 9; n > d * count; { n = n - count * d d++ count = count * 10 } //原创 2022-04-26 10:46:01 · 1244 阅读 · 0 评论 -
Leetcode 378. 有序矩阵中第 K 小的元素
Leetcode 378. 有序矩阵中第 K 小的元素1. 问题描述2. 思路左上角元素作为left,右下角元素作为right执行二分查找3. 代码func kthSmallest(matrix [][]int, k int) int { n := len(matrix) left, right := matrix[0][0], matrix[n-1][n-1] for left <= right { mid := left +原创 2022-04-25 17:56:38 · 523 阅读 · 0 评论 -
Leetcode153. 寻找旋转排序数组中的最小值
Leetcode153. 寻找旋转排序数组中的最小值1. 问题描述2. 思路该问题实际上就是在查找数列的“断点”位置二分查找,通过比较nums[mid] 和 nums[0]不断逼近断点位置3. 代码func findMin(nums []int) int { left, right := 0, len(nums) - 1 if nums[right] >= nums[left] { return nums[left] } for lef原创 2022-04-22 10:23:17 · 398 阅读 · 0 评论 -
LeetCode543 二叉树的直径
LeetCode543 二叉树的直径问题描述思路每个节点的直径为,左右子树最大深度之和代码/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func diameterOfBinaryTree(root *TreeNode) int { var res int原创 2021-12-24 10:45:59 · 323 阅读 · 0 评论 -
LeetCode1019. 链表中的下一个更大节点
LeetCode1019. 链表中的下一个更大节点1. 问题描述2. 思路2.1. 思路一存到数组中2.2. 思路二反转链表单调栈3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func nextLargerNodes(head *ListNode) []int { var res [原创 2021-11-25 11:18:56 · 403 阅读 · 0 评论 -
LeetCode 876. 链表的中间结点
LeetCode 876. 链表的中间结点1. 问题描述2. 思路快慢指针fast = fast.Next.Next; slow = slow.Next3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func middleNode(head *ListNode) *ListNode { slo原创 2021-11-25 10:18:41 · 3986 阅读 · 0 评论 -
LeetCode817. 链表组件
LeetCode817. 链表组件1. 问题描述2. 思路3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func numComponents(head *ListNode, nums []int) int { var res int numsCounter := map[int]int{}原创 2021-11-24 21:04:30 · 273 阅读 · 0 评论 -
LeetCode430. 扁平化多级双向链表
LeetCode430. 扁平化多级双向链表1. 问题描述2. 思路3. 代码/** * Definition for a Node. * type Node struct { * Val int * Prev *Node * Next *Node * Child *Node * } */func flatten(root *Node) *Node { dfs(root) return root}func dfs(nod原创 2021-11-23 17:37:33 · 251 阅读 · 0 评论 -
LeetCode725. 分隔链表
LeetCode725. 分隔链表1. 问题描述2. 思路3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func splitListToParts(head *ListNode, k int) []*ListNode { length := 0 for node := head; node原创 2021-11-23 10:25:39 · 261 阅读 · 0 评论 -
LeetCode445. 两数相加II
LeetCode445. 两数相加II1. 问题描述2. 思路本题的主要难点在于链表中数位的顺序与我们做加法的顺序是相反的,为了逆序处理所有数位,我们可以使用栈:把所有数字压入栈中,再依次取出相加。计算过程中需要注意进位的情况。3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func addTwoNumber原创 2021-11-22 11:14:48 · 299 阅读 · 0 评论 -
LeetCode328. 奇偶链表
LeetCode328. 奇偶链表1. 问题描述2. 思路双指针odd指向奇数位,even指向偶数位head指向奇数位的头部,evenHead指向偶数位的头部将奇数链表和偶数链表连接起来3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func oddEvenList(head *ListNode)原创 2021-11-21 15:33:04 · 4004 阅读 · 0 评论 -
LeetCode147. 对链表进行插入排序
LeetCode147. 对链表进行插入排序1. 问题描述2. 思路遍历链表,找到开始下降的断点处1.1. 遍历链表找到要插入的位置插入3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func insertionSortList(head *ListNode) *ListNode { head原创 2021-11-19 09:29:06 · 243 阅读 · 0 评论 -
LeetCode968. 监控二叉树
LeetCode968. 监控二叉树1. 问题描述2. 思路https://leetcode-cn.com/problems/binary-tree-cameras/solution/968-jian-kong-er-cha-shu-di-gui-shang-de-zhuang-ta/3. 代码/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeN原创 2021-11-18 10:28:58 · 161 阅读 · 0 评论 -
LeetCode138. 复制带随机指针的链表
LeetCode138. 复制带随机指针的链表1. 问题描述2. 思路2.1. 思路一将该链表中每一个节点拆分为两个相连的节点,例如对于链表 A→B→C,我们可以将其拆分为 A→A ′ →B→B ′ →C→C ′我们可以直接找到每一个拷贝节点 S ′ 的随机指针应当指向的节点,即为其原节点 SS 的随机指针指向的节点 T的后继节点 T ′将重装好的链表进行拆分2.2. 思路二声明一个map[*Node]*Node类型的map遍历链表,以node为key,当前node的值为Valu原创 2021-11-17 21:18:51 · 263 阅读 · 0 评论 -
缺失的元素系列
缺失的元素1.package main// leetcode1539. 第k个缺失的正整数// 输入:arr = [2,3,4,7,11], k = 5// 输出:9// 解释:缺失的正整数包括 [1,5,6,8,9,10,12,13,...] 。第 5 个缺失的正整数为 9 。// 根据数组中每个数所对应的缺失的数的个数来考虑func find1(nums []int, k int) int { for i := 0; i < len(nums); i++ { if num原创 2021-11-17 11:21:47 · 441 阅读 · 0 评论 -
LeetCode143. 重排链表
LeetCode143. 重排链表1. 问题描述2. 思路把排序链表的node转存到数组中将数组首尾元素依次取出,重组链表3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func reorderList(head *ListNode) { nodes := []*ListNode{}原创 2021-11-16 20:44:19 · 4460 阅读 · 0 评论 -
LeetCode82. 删除排序链表中的重复元素II
LeetCode82. 删除排序链表中的重复元素II1. 问题描述2. 思路 声明一个header指向head节点,每次向后看两步,比较cur.Next.Val 和 cur.Next.Next.Val是否相等。3. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func deleteDuplicates(hea原创 2021-11-15 14:13:32 · 445 阅读 · 0 评论 -
LeetCode24. 两两交换链表中的节点
LeetCode24. 两两交换链表中的节点1. 问题描述2. 思路p1.Next = p2.Nextp2.Next = p2.Next.Nextp1.Next.Next = p23. 代码/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func swapPairs(head *ListNode) *ListN原创 2021-11-14 21:32:13 · 253 阅读 · 0 评论