Skip to content
  • Sponsor GoogTech/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 4

Commit 110010c

Browse files
committedSep 15, 2020
🎉 #1557. Minimum Number of Vertices to Reach All Nodes
1 parent c0bf948 commit 110010c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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]

0 commit comments

Comments
 (0)
Please sign in to comment.