051 N Queens
此题即在 052 N Queens 2 基础上稍作改进 并注意返回格式就好
import copy class Solution: def __init__(self): self.ans = [] # @param {integer} n # @return {integer} def solveNQueens(self, n): self.helper(0,[["."]* n for i in range(0,n)],n) return self.ans def helper(self, level, tmp, n): if level == n: a = [] for t in tmp: a.append("".join(t)) self.ans.append(a) return for pos in range(0,n): flag = True for i in range(0, level): if not flag: break if tmp[i][pos] == "Q": flag = False break for j in range(0, n): if tmp[i][j]== "Q" and (level-i == pos-j or level-i==j-pos): flag = False break if flag: tmp[level][pos] = "Q" self.helper(level+1, tmp, n) tmp[level][pos] = "."