1 line Python solution

  • 2
        def solveNQueens(self, n):
            return [['.' * v + 'Q' + '.' * (n - v - 1) for v in c] for c in itertools.permutations(range(n))
                    if (len(set(i + v for i, v in enumerate(c))) == n) and
                    (len(set(i - v for i, v in enumerate(c))) == n)]

  • 0

    This is brilliant

  • 1

    I never get the point of these one liners . it looks slick but hard to understand, and you have to break it down to understand it.

    if you use this one liner in a project and other engineers have to break down everything you code, then that would be a problem .

    writing "longer" but easy to understand code is a better idea

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.