52. N-Queens II Leetcode Python

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

这题的解法是建一个board表示行比如n=4 时候board=[1,3,0,2] 表示   0Q00,000Q,Q000,00Q0

利用对于dfs每次确认当前Board是否满足条件时候已经表示之前的格子都已经满足条件了。

在check 函数表示 queen 是否可以放在第k(depth)行j(row)列

class Solution:
    # @return an integer
    def __init__(self):
        self.count=0
    def check(self,k,j,board):
        for i in range(k):
            if board[i]==j or abs(k-i)==abs(board[i]-j):
                return False
        return True
    def dfs(self,depth,board,n):
        if depth==n:
            self.count+=1
        for row in range(n):
            if self.check(depth,row,board):
                s='.'*n
                board[depth]=row
                self.dfs(depth+1,board,n)
    def totalNQueens(self, n):
        board=[-1 for i in range(n)]
        self.dfs(0,board,n)
        return self.count
        
时间: 2024-12-18 23:08:27

52. N-Queens II Leetcode Python的相关文章

Pascal's triangle II Leetcode Python

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题和前面一题的区别在于不用返回所有解,只需要返回index对应行就行.做法和前面的一样定义一个currow 和prerow class Solu

45. Jump Game II Leetcode Python

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

63. Unique Path II Leetcode Python

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

Populating Next Right Pointers in Each Node II Leetcode Python

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

82. Remove Duplicates from Sorted List II Leetcode Python

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

[Leetcode][Python]52: N-Queens II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 52: N-Queens IIhttps://oj.leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. ===Comm

leetcode 【 Pascal's Triangle II 】python 实现

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

[leetcode] 52. N皇后 II

52. N皇后 II 跟上个题一模一样,现在只需输出个数即可 class Solution { public int totalNQueens(int n) { boolean[] row = new boolean[n]; boolean[] h = new boolean[2 * n]; boolean[] r = new boolean[2 * n]; List<List<String>> ans = new ArrayList<>(); dfs(n, row,

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =