[LeetCode]题解(python):051-N-Queens

题目来源:

  https://leetcode.com/problems/n-queens/



题意分析:

  这是一个N-后问题。在一个N×N的国际象棋板上放N个皇后使得这些皇后使得他们互相不能攻击。也就是一个皇后的行列和斜都没有其他的皇后。返回所有满足上述条件的所有结果。



题目思路:

  这题和前面的数独类似。首先要确定添加一个皇后的时候判断是否符合条件。利用回溯法每行或者每列添加皇后。



代码(python):

  

class Solution(object):
    def solveNQueens(self, n):
        """
        :type n: int
        :rtype: List[List[str]]
        """
        def isqueens(depth,j):
            for i in range(depth):
                if board[i] == j or abs(depth - i) == abs(board[i] - j):
                    return False
            return True
        def dfs(depth,row):
            if depth == n:
                ans.append(row);return
            for i in range(n):
                if isqueens(depth,i):
                    board[depth]= i
                    dfs(depth + 1,row + [‘.‘*i + ‘Q‘ + ‘.‘*(n - i - 1)])
        board = [-1 for i in range(n)]
        ans = []
        dfs(0,[])
        return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4968648.html

时间: 2024-10-12 15:07:24

[LeetCode]题解(python):051-N-Queens的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

(leetcode题解)Pascal's Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[leetcode]Candy @ Python

原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

LeetCode: N-Queens II [051]

[题目] Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. [题意] 解N皇后问题,N-Queens要求返回所有的解,而本题只需要返回可行解的数目 [思路] DFS,参考N-Queens [代码] class Solution { public: bool isValid(vector<string

[leetcode]4Sum @ Python

原题地址:http://oj.leetcode.com/problems/4sum/ 题意:从数组中找到4个数,使它们的和为target.要求去重,可能有多组解,需要都找出来. 解题思路:一开始想要像3Sum那样去解题,时间复杂度为O(N^3),可无论怎么写都是Time Limited Exceeded.而同样的算法使用C++是可以通过的.说明Python的执行速度比C++慢很多.还说明了一点,大概出题人的意思不是要让我们去像3Sum那样去解题,否则这道题就出的没有意义了.这里参考了kitt的解

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod