![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode Graph
Tech In Pieces
这个作者很懒,什么都没留下…
展开
-
LeetCode在解决图相关的问题的一些深入的思考
1 DFS和BFS 说到底是遍历所有的节点的算法,而visited这个辅助存储 就是为了防止再走之前走过的节点。但是如果我们有个问题 需要遍历所有的边怎么办?(就像332. Reconstruct Itinerary 我们要遍历所有的边 而且只有一次 返回最小的字典序的节点组合)2 针对自己不想用DFS的问题(主要是想递归实在太蛋疼)而且有的时候 明明是DFS更适合 却一定要用BFS。其实自己仔细想一下 dfs也没啥大问题 只要想清楚我们这个DFS函数的含义就行了,至于里面的参数 无非就是Map,visi原创 2020-12-23 10:28:13 · 56 阅读 · 0 评论 -
Revisited Tree and Graph related problems(Old Version)
I’m terrified of such kind of problems. so I write this because I really want to know how to solve such problems…many tree or graph realted problems are not that hard, they are easily to understand, ...原创 2020-12-11 08:26:32 · 733 阅读 · 0 评论 -
LeetCode 1519. Number of Nodes in the Sub-Tree With the Same Label
given a tree, containing n nodes and n-1 edges. the root is node 0 and each node has a label(character), that means The node with the number i has the label labels[i].The edges array is given on the form edges[i] = [ai, bi], which means there is an edge b原创 2020-12-04 23:25:26 · 256 阅读 · 0 评论 -
LeetCode 332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.idea:we have a list of原创 2020-12-03 13:02:16 · 186 阅读 · 0 评论 -
LeetCode 787. Cheapest Flights Within K Stops?
There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w.Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to ds原创 2020-12-03 10:23:26 · 166 阅读 · 0 评论 -
图的构建方法(Java)
不管是LeetCode中 还是在实际的OA中 我们都会遇到许多关于图的问题 而很多问题中 构建合适的图是第一步 也是非常重要的一步。那么有哪些图的构建方法呢?用HashMap<Node, List> map来表示Node以及他的邻居节点。这种表示方式可以表示无向图和有向图,但是要注意在构建无向图的时候 每条边要加两次。或者用2D array: 邻接表 虽然浪费了点空间 而且这种方式可以用来构建鲉形图或者无向图。关于其他的辅助储存:比如说:BFS中我们经常需要使用visited来判断原创 2020-12-03 07:32:20 · 1506 阅读 · 0 评论 -
LeetCode 1202. Smallest String With Swaps
You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.You can swap the characters at any pair of indices in the given pairs any number of times.Return the lexic原创 2020-12-02 13:06:34 · 150 阅读 · 0 评论 -
LeetCode 1466. Reorder Routes to Make All Paths Lead to the City Zero?
There are n cities numbered from 0 to n-1 and n-1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are to原创 2020-12-01 13:01:20 · 673 阅读 · 0 评论 -
LeetCode 1462. Course Schedule IV
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have direct prerequisites, for example, to take course 0 you have first to take course 1, which is expressed as a pair: [1,0]Given the total number of courses n, a li原创 2020-12-01 09:49:06 · 138 阅读 · 0 评论 -
LeetCode 886. Possible Bipartition?
Given a set of N people (numbered 1, 2, …, N), we would like to split everyone into two groups of any size.Each person may dislike some other people, and they should not go into the same group.Formally, if dislikes[i] = [a, b], it means it is not allowed原创 2020-11-30 12:56:13 · 136 阅读 · 0 评论 -
LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??
There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.Return the city with the smallest nu原创 2020-11-30 05:51:20 · 196 阅读 · 0 评论 -
LeetCode 133. Clone Graph
deep copy of a graph.注意 所谓的deep copy,就是对每一个节点 进行 new Node(node.val, List neighbor)的复制clearly, use BFS: queue and visited因为我们需要复制 而本来 我们只需要visited是hashset就行 就是BFS的便利的时候边加便检查就行。但是我们现在需要deep copy,所以我们用hashmap来 采用oldnode,newnode键值对的方式。class Solution {原创 2020-11-29 12:24:19 · 125 阅读 · 0 评论 -
LeetCode 997. Find the Town Judge
就像之前见过的那道:名人不认识任何其他人 但是所有的其他人都认识这个名人。现在的规则是法官不相信任何人 但是所有其他人都相信这个法官 这些人之间是否相互信任并不确定。In a town, there are N people labelled from 1 to N.You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labe原创 2020-11-29 11:41:53 · 68 阅读 · 0 评论 -
LeetCode 990. Satisfiability of Equality Equations
given an array of equations and nonequations, check if all the equations are true. if any of them is false then return false;Input: [“a==b”,“b!=a”]Output: falseremember: we will only contains == and != operator.idea:这种题目要用图的思想去想:由于具有传递姓,比如ab and ca,原创 2020-11-29 10:32:44 · 138 阅读 · 0 评论 -
LeetCode图 常见的题型以及要注意的点
图的题目一直是pain in the ass,虽然总体来说只有两个算法来解决这个问题:DFS和BFS 常用的数据结构有:hashmap此外 要注意某一些能用并查集去做的题目。而且也要注意那些看上去并不属于图的题目 也很有可能可以用图去做。由于图的题目变化多端,所以如何对图的题目进行整理分类?BFS/DFS...原创 2020-11-28 13:08:34 · 170 阅读 · 0 评论 -
LeetCode 拓扑排序题目以及要注意的点
207 Course schedule210 course schedule 2269 Alien dictionary329 longest increasing path in a matrix444 sequence reconstruction1203 sort items by groups respecting dependencies都是高频题目遇到这种题目,想一下:什么数据结构用的比较多?什么样的手法(技巧)用的比较多?有哪些需要 注意的点?数据结构:HashMap(Hash原创 2020-11-28 09:32:30 · 272 阅读 · 0 评论 -
LeetCode 444 sequence reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 10^4. Reconstruction means building a shortest common supersequence of the sequenc原创 2020-11-28 05:50:27 · 228 阅读 · 1 评论 -
LeetCode 329 longest increasing path in a matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down.Input: nums =[[9,9,4],[6,6,8],[2,1,1]]Output: 4这道题乍一看上去 就像是LIS LCS问题,但是因为可以往上下左右移动 所以这有点像DP题目而原创 2020-11-27 12:18:31 · 137 阅读 · 0 评论 -
LeetCode 269. Alien Dictionary
You are given a list of strings words from the dictionary, where words are sorted lexicographically by the rules of this new language.Derive the order of letters in this language, and return it. If the given input is invalid, return “”. If there are multi原创 2020-11-27 10:38:11 · 131 阅读 · 0 评论 -
LeetCode 207 Course Schedule
given a 2D array, represents for the class and it’s prerequesite classlike [0,1], it means that we should take class 1 before class 0.public boolean canFinish(int numCourses, int[][] prerequisites)return if it is possible for us to finish all the cours原创 2020-11-27 06:36:29 · 188 阅读 · 0 评论 -
如何检测图中的环?
从每个节点出发 判断从这个节点出发DFS 最后是不是又经过了这个节点(visited)以LC207 Course Schedule为例HashMap<Integer, List<Integer>> courseDict = new HashMap<>();boolean[] visited = new boolean[numCourses];for (int i = 0; i < numCourses; i++) { //pay attention, thi原创 2020-11-27 05:48:33 · 631 阅读 · 0 评论