LeetCode N皇后 & N皇后 II

题目链接:https://leetcode-cn.com/problems/n-queens/

题目链接:https://leetcode-cn.com/problems/n-queens-ii/

题目大意:

  略。

分析:

  https://blog.csdn.net/kai_wei_zhang/article/details/8033194

代码如下:

 1 class Solution {
 2 public:
 3     int allOne;
 4     vector<vector<string>> ans;
 5     vector<string> ret;
 6     string tmp;
 7     int N;
 8
 9     vector<vector<string>> solveNQueens(int n) {
10         allOne = (1 << n) - 1;
11         ans.clear();
12         tmp.assign(n, ‘.‘);
13         ret.assign(n, tmp);
14         N = n;
15
16         solve(0, 0, 0, 0);
17
18         return ans;
19     }
20
21     // vd : 竖直方向
22     // ld : 左斜线方向
23     // rd : 右斜线方向
24     void solve(int level, int vd, int ld, int rd) {
25         if(level == N) {
26             ans.push_back(ret);
27             return;
28         }
29
30         int limit = (vd | ld | rd) ^ allOne;
31         int pos;
32
33         while(limit) {
34             pos = lowbit(limit);
35             limit = cutLowbit(limit);
36
37             ret[level][N - __builtin_ffs(pos)] = ‘Q‘;
38             solve(level + 1, vd | pos, ((ld | pos) >> 1) & allOne, ((rd | pos) << 1) & allOne);
39             ret[level][N - __builtin_ffs(pos)] = ‘.‘;
40         }
41     }
42
43     int lowbit(int x) {
44         return x & (-x);
45     }
46
47     int cutLowbit(int x) {
48         return x & (x - 1);
49     }
50 };

 1 class Solution {
 2 public:
 3     int allOne;
 4     int ans;
 5     int N;
 6
 7     int totalNQueens(int n) {
 8         allOne = (1 << n) - 1;
 9         ans = 0;
10         N = n;
11
12         solve(0, 0, 0, 0);
13
14         return ans;
15     }
16
17     // vd : 竖直方向
18     // ld : 左斜线方向
19     // rd : 右斜线方向
20     void solve(int level, int vd, int ld, int rd) {
21         if(level == N) {
22             ++ans;
23             return;
24         }
25
26         int limit = (vd | ld | rd) ^ allOne;
27         int pos;
28
29         while(limit) {
30             pos = lowbit(limit);
31             limit = cutLowbit(limit);
32
33             solve(level + 1, vd | pos, ((ld | pos) >> 1) & allOne, ((rd | pos) << 1) & allOne);
34         }
35     }
36
37     int lowbit(int x) {
38         return x & (-x);
39     }
40
41     int cutLowbit(int x) {
42         return x & (x - 1);
43     }
44 };

原文地址:https://www.cnblogs.com/zaq19970105/p/11410040.html

时间: 2024-10-12 00:00:15

LeetCode N皇后 & N皇后 II的相关文章

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

leetcode 【 Pascal&#39;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] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private

leetcode 119 Pascal&#39;s Triangle II ----- java

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? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

leetCode 119. Pascal&#39;s Triangle II 数组

119. Pascal's Triangle II 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? 代码如下:(使用双数组处理,未优化版) class Solution { public:     

LeetCode#119 Pascal&#39;s Triangle II

Problem Definition: 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? Solution: 1 def getRow(rowIndex): 2 pt=[] 3 for rn in

[leetcode] 2. Pascal&#39;s Triangle II

我是按难度往下刷的,第二道是帕斯卡三角形二.简单易懂,题目如下: 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? 就是输入k然后输出第k行帕斯卡三角[其实我一直把它叫杨辉三角]. 我这边思路是拿

[LeetCode 题解]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

LeetCode OJ - Reverse Linked List II

题目: 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 ≤ l

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #