[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, h, r, new ArrayList<>(), 0, ans);
        return ans.size();
    }

    public void dfs(int n, boolean[] row, boolean[] h, boolean[] r, List<Integer> curList, int curK, List<List<String>> ans) {
        if (curK == n) {
            List<String> tmp = new ArrayList<>();
            for (Integer integer : curList) {
                String st = "";
                for (int i = 0; i < n; i++) {
                    if (i == integer) {
                        st += "Q";
                    } else {
                        st += ".";
                    }
                }
                tmp.add(st);
            }
            ans.add(tmp);
        }

        for (Integer i = 0; i < n; i++) {
            if (!row[i] && !h[curK + i] && !r[curK - i + n]) {
                row[i] = true;
                h[curK + i] = true;
                r[curK - i + n] = true;
                curList.add(i);
                dfs(n, row, h, r, curList, curK + 1, ans);
                curList.remove(i);
                row[i] = false;
                h[curK + i] = false;
                r[curK - i + n] = false;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/acbingo/p/9352243.html

时间: 2024-10-29 02:39:35

[leetcode] 52. N皇后 II的相关文章

leetcode 52 N皇后问题 II

51的简化版,省去根据排列话棋盘的工作,直接计数,代码: class Solution { public: int totalNQueens(int n) { int res=0; vector<int> pos(n,-1); dfs(n,0,pos,res); return res; } void dfs(int n,int row,vector<int>& pos,int &res){ if(row==n){ res++;return; } for(int co

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

leetcode之n皇后问题

leetcode上有两个关于n皇后的问题,两个题目基本是一样的,只是第二个是把所有的排法求出来.n皇后最简单的就是用递归,每次判断一行的一个位置,如果合法,就判断下一行,不合法再判断下一个位置 N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. class Solution {

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II 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

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同