【leetcode】841. Keys and Rooms

提交代码

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_第1张图片

你可能感兴趣的:(leetcode)