LeetBook--队列&栈--钥匙和房间
This commit is contained in:
parent
74327b8c3b
commit
073c59ae80
42
src/main/java/leet/book/queueStack/CanVisitAllRooms.java
Normal file
42
src/main/java/leet/book/queueStack/CanVisitAllRooms.java
Normal file
@ -0,0 +1,42 @@
|
||||
package leet.book.queueStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: 轩辕龙儿
|
||||
* @date: 2021/7/2 16:09
|
||||
* @Description: No Description
|
||||
*/
|
||||
public class CanVisitAllRooms {
|
||||
class Solution {
|
||||
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
|
||||
int num = rooms.size();
|
||||
List<Integer> use = new ArrayList<>();
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
queue.offer(0);
|
||||
use.add(0);
|
||||
if (num == use.size()) {
|
||||
return true;
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
int open = queue.poll();
|
||||
for (int room : rooms.get(open)) {
|
||||
if (use.contains(room)) {
|
||||
continue;
|
||||
}
|
||||
queue.offer(room);
|
||||
use.add(room);
|
||||
if (num == use.size()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user