欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【leetcode】841. Keys and Rooms

程序员文章站 2022-06-02 23:30:49
...

提交代码

class Solution {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] access=new boolean[rooms.size()];
        Arrays.fill(access, false);
        access[0] = true;
        Queue<Integer> next=new LinkedList<>();
        next.add(0);
        while(next.size()>0) {
        	int curRoom = next.poll();
        	for(int i=0;i<rooms.get(curRoom).size();i++) {
        		if(access[rooms.get(curRoom).get(i)])
        			continue;
        		access[rooms.get(curRoom).get(i)] = true;
        		next.add(rooms.get(curRoom).get(i));
        	}
        }
        for(int i=0;i<access.length;i++)
        	if(!access[i])
        		return false;
        return true;
    }
}

运行结果

【leetcode】841. Keys and Rooms

相关标签: leetcode