51. N-Queens

# Hard

Think it as permutation, add one eligible element,continue picking next, but don't forget to remove it. This can be regarded as Bruce Force, trying all cases eventually.

只要结果的长度都是n的数组,那么就是“排列”的方法。横着、纵着、斜都会互相attack。

Solution:

  1. helper function, similar as @60, but adding one judge if element is valid.

  2. isValid function, new adding element must not conflict with existed elements. Return True or False.

  3. stop condition in helper function is to length, we transfer numerical array to string list by the way

Conditions of isValid:

  • column_A != column_B

  • row_A + column_A != row_B + column_B

  • row_A - column_A != row_B - column_B

Last updated