03 2021 档案
摘要: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
阅读全文
摘要:二叉树遍历可以使用递归/非递归的方式完成,对于递归,最简洁的思路是模拟栈的工作,使用一个辅助栈来完成,从而时间复杂度O(N)空间复杂度也是O(N)。 那么有没有一种时间复杂度O(N)但是空间复杂度O(1)的方法呢? Morris Traversal 可以做到这两点,它使用叶结点的子结点为空这一特性来
阅读全文
摘要:用户态IO:DPDK 在讲多路复用的时候,我们提到:多路复用本身仍然是同步I/O,数据从内核空间到用户空间仍然存在一次拷贝,影响效率。其实对于大容量网络通信(如交换机)而言,不仅数据拷贝的开销不可接受,系统调用频繁的上下文切换也是难以忽视的开销。 Intel DPDK(Data Plane Deve
阅读全文
摘要:I/O 多路复用(I/O multiplexing) 本文对阻塞/非阻塞、同步/异步等概念进行简要区分,简单介绍了5种I/O模型,对Linux平台3种I/O复用的方式进行了比较,重点分析了 epoll 的实现。 I/O 模型 得益于Unix“一切皆文件”的设计哲学,I/O操作本身通过 read/wr
阅读全文
摘要:两道环形队列设计的题。 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
阅读全文

浙公网安备 33010602011771号