Leetcode——841. Keys and Rooms

题目原址

https://leetcode.com/problems/keys-and-rooms/description/

解题思路

经典DFS题

AC代码

class Solution {
    public boolean canVisitAllRooms(List> rooms) {
        boolean[] room = new boolean[rooms.size()];
        dfs(rooms, room, 0);
        for(boolean r: room) {
            if(!r)
                return false;
        }
        return true;
    }

    private void dfs(List> rooms, boolean[] room, int i) {
        room[i] = true;
        List list = rooms.get(i);
        for(int j: list) {
            if(! room[j]) {
                dfs(rooms, room, j);
            }
        }        
    }
}

感谢

https://leetcode.com/problems/keys-and-rooms/discuss/138293/Java-DFS-7ms

你可能感兴趣的:(LeetCode,Leetcode_DFS)