随笔分类 - LeetCode
摘要:LeetCode 786. K-th Smallest Prime Fraction 一道经典题,给出两种经典解法。 题目描述 You are given a sorted integer array arr containing 1 and prime numbers, where all the
阅读全文
摘要:LeetCode 378. Kth Smallest Element in a Sorted Matrix 一道经典的二分查找的题目,特点在于查找对象从一位有序数组变成了二位行列有序数组。 题目描述 Given an n x n matrix where each of the rows and c
阅读全文
摘要:priority_queue 自定义 comparator LeetCode 373. Find K Pairs with Smallest Sums 本文重点记录需要自定义 comparator 时的priority_queue 的写法。 题目描述 You are given two intege
阅读全文
摘要:原地调整法查找数组元素 题目描述 这一类题目一般是给出一个数组,数组元素的取值范围在 1~n 之间,其中 n 是数组元素个数,然后要求找出缺失元素或者重复元素,缺失或重复的元素可以是一个或多个。例如: LeetCode 448. Find All Numbers Disappeared in an
阅读全文
摘要:LeetCode 436. Find Right Interval 一道需要自定义比较函数的二分查找题。 这里重点展示自定义比较,给出比较函数、比较器、lambda三种写法。 题目描述 You are given an array of intervals, where intervals[i] =
阅读全文
摘要:LeetCode 611. Valid Triangle Number 题目描述 Given an array consists of non-negative integers, your task is to count the number of triplets chosen from th
阅读全文
摘要:两道环形队列设计的题。 LeetCode 622. Design Circular Queue LeetCode 641. Design Circular Deque 题目描述 一道题是环形 queue 的设计,circular queue is also called "Ring Buffer";
阅读全文
摘要:LeetCode 上两道相似的 hash 数据结构设计题。 LeetCode 705. Design HashSet LeetCode 706. Design HashMap 题目描述 两道题题干相似,都是给出接口要求实现数据结构。两道题都做出了一些简化以降低设计难度: key / value 都用
阅读全文
摘要:水塘抽样 与 洗牌算法 本文介绍两个相似的问题,水塘抽样和洗牌算法。 水塘抽样(Reservoir Sampling) 水塘抽样(Reservoir Sampling)说的是这样一个问题:当内存无法完全加载时,如何从数据流或大数据集中随机选取k个样本,并保证每个样本被选取的概率相等。 典型问题出现在
阅读全文
摘要:LeetCode 279. Perfect Squres DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题。 题目描述 Given a positive integer n, find the least number of perfect square numbers (for e
阅读全文
摘要:LeetCode 1025. 除数博弈 博弈问题,非常有趣。 题目描述 Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number N on the chalkboa
阅读全文
摘要:LeetCode 679. 24点游戏 看到一道回溯的题目,一时有点毛爪,看leetcode的讨论,竟然发现 打表 和 暴力枚举 的做法。受此启发写下了这份答案。 题目描述 You have 4 cards each containing a number from 1 to 9. You need
阅读全文
摘要:阿里暑期实习笔试题 阿里暑期实习的笔试,3月6日的第一场据说比较难,两道 leetcode hard题。 3.9 更新 找到问题了,问题出在 auto&& [stop, step] = q.front(); q.pop(); 这两句上,我们使用的是一个引用,但是后来这个元素被 pop 释放掉了,所以
阅读全文
摘要:跳跃游戏系列(LeetCode Jump Game I-V) 用到的思路包括:贪心、广搜、递归。 题目较多,就不贴原题了,点击标题直接跳转查看。 LeetCode 55. Jump Game I 这道题给出的数组元素表示当前位置最大可跳距离,问能否从第一个位置到达最后一个位置。 BFS 是会超时的,
阅读全文
摘要:LeetCode 396. Rotate Function 一道数学题。 题目描述 Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the a
阅读全文
摘要:LeetCode 240. Search a 2D Matrix II 一道经典的二维矩阵搜索题。 题目描述 Write an efficient algorithm that searches for a target value in an m x n integer matrix. The m
阅读全文
摘要:LeetCode 44. 通配符匹配 一道简化版的正则表达式匹配题。 题目描述 Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'
阅读全文
摘要:LeetCode 10. 正则表达式匹配 这道题是正则表达式的匹配,初始化和状态转移都比较繁琐,需要非常小心,测试样例也给的比较全一些。先做一道简化版的 LeetCode 44. 通配符匹配 会简单一些。 题目描述 Given an input string (s) and a pattern (p
阅读全文
摘要:LeetCode 84. 柱状图中最大的矩形 题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 求在该柱状图中,能够勾勒出来的矩形的最大面积。 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。 图中阴影部分为所
阅读全文
摘要:LeetCode 42. 接雨水 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例 1: 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] 输出:6 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,
阅读全文

浙公网安备 33010602011771号