File tree 2 files changed +44
-0
lines changed
#1557. Minimum Number of Vertices to Reach All Nodes
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Author: Goog Tech
3
+ * @Date: 2020-09-15 16:12:07
4
+ * @LastEditTime: 2020-09-15 16:12:27
5
+ * @Description: https://leetcode-cn.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
6
+ * @FilePath: \leetcode-googtech\#1557. Minimum Number of Vertices to Reach All Nodes\Solution.java
7
+ * @WebSite: https://algorithm.show/
8
+ */
9
+
10
+ class Solution {
11
+ // 解题思路 : 遍历所有的边,使用集合存储所有有向边的终点,
12
+ // 集合中的所有节点即为入度不为零的节点,剩下的所有节点即为入度为零,即没有前驱的节点.
13
+ public List <Integer > findSmallestSetOfVertices (int n , List <List <Integer >> edges ) {
14
+ List <Integer > resultList = new ArrayList <>();
15
+ Set <Integer > endingSet = new HashSet <>();
16
+ for (List <Integer > edge : edges ) endingSet .add (edge .get (1 ));
17
+ for (int i = 0 ; i < n ; i ++) {
18
+ if (!endingSet .contains (i )) {
19
+ resultList .add (i );
20
+ }
21
+ }
22
+ return resultList ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ '''
2
+ Author: Goog Tech
3
+ Date: 2020-09-15 16:12:13
4
+ LastEditTime: 2020-09-15 16:12:39
5
+ Description: https://leetcode-cn.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
6
+ FilePath: \leetcode-googtech\#1557. Minimum Number of Vertices to Reach All Nodes\Solution.py
7
+ WebSite: https://algorithm.show/
8
+ '''
9
+
10
+ class Solution (object ):
11
+ # 解题思路 : 遍历所有的边,使用集合存储所有有向边的终点,
12
+ # 集合中的所有节点即为入度不为零的节点,剩下的所有节点即为入度为零,即没有前驱的节点.
13
+ def findSmallestSetOfVertices (self , n , edges ):
14
+ """
15
+ :type n: int
16
+ :type edges: List[List[int]]
17
+ :rtype: List[int]
18
+ """
19
+ endingSet = set (y for x , y in edges )
20
+ return [i for i in range (n ) if i not in endingSet ]
You can’t perform that action at this time.
0 commit comments