UnionFind unionFind = new UnionFind(2 * equationsSize); // 第 1 步:预处理,将变量的值与 id 进行映射,使得并查集的底层使用数组实现(用唯一id来代替字符),方便编码 Map<String, Integer> hashMap = new HashMap<>(2 * equationsSize); int id = 0; for (int i = 0; i < equationsSize; i+...
for(inti =0; i < len; ++i) nodes[i] =newNode(i); // union, you can take an example as follows // (a/b=3)->(c/d=6)->(b/d=12) for(inti =0; i < equations.length; ++i) { String[] keys = equations[i]; intk1 = mIdTable.get(keys[0]); intk2 = mIdTable.get(ke...
Cyclic Sort (easy) Find the Missing Number (easy) Find all Missing Numbers (easy) Find the Duplicate Number (easy) Find all Duplicate Numbers (easy) 6. Pattern: In-place Reversal of a LinkedList,链表翻转 在众多问题中,题目可能需要你去翻转链表中某一段的节点。通常,要求都是你得原地翻转,就是...
Leetcode 310 Minimum Height Trees Leetcode 366 Find Leaves of Binary Tree 深度优先搜索(DFS):面试中最常考的(分类的稍微有点粗糙了,没有细分出回溯/分治来,准备找个时间给每个DFS的题标记下是哪种DFS) 基础知识: 常见的DFS用来解决什么问题?(1) 图中(有向无向皆可)的符合某种特征(比如最长)的路径以及...
并查集(Union Find) 基础知识:如果数据不是实时变化,本类问题可以用BFS或者DFS的方式遍历,如果数据实时变化(data stream)则并查集每次的时间复杂度可以视为O(1);需要牢记合并与查找两个操作的模板 常见题目: Leetcode 990 Satisfiability of Equality Equations ...
很多人连Segment Tree,BIT,Trie,Union Find这种数据结构都不懂,遇见就刷,自己想,怎么可能想的出来...
684 Redundant Connection C++ Python O(n) O(n) Medium Union Find 685 Redundant Connection II C++ Python O(n) O(n) Hard Union Find 687 Longest Univalue Path C++ Python O(n) O(h) Easy 699 Falling Squares C++ Python O(nlogn) O(n) Hard Segment Tree Hash Table #TitleSolutionTimeSpace...
可以叫我 weiwei,在我力所能及且时间允许的情况下,我会尽可能回答我知道的问题。如果有不能及时回复的问题,可能是因为我没有看到站内通知,可以发邮件给我liweiwei1419@gmail.com。 我录制的视频题解 我从2019 年 9 月开始录制视频题解。最开始的时候,我会对着要讲的材料录好几遍。现在讲解知识点的时候写逐字...
可以用Union Find做,但其实用DFS做更直接。 这就是一个典型的图遍历的问题。每条边都是有weight的。 狗家喜欢考图。这就是常见的图问题。 挺好的 转载于:https://www.jianshu.com/p/9f1da3289f46...399. Evaluate Division 转载于:https://www.jianshu.com/p/6a5b3fd5c57b...399. Evaluate ...
Solution 2: Union FindC++ Java Python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // Author: Huahua // Runtime: 3 ms class Solution {...