【LeetCode】52.N-Queens II

用的是回溯法

 1 class Solution {
 2 public:
 3     int INITIAL = -1000;
 4     // 一维数组a[n]中,下标代表行号,对应里面存的值代表列号。比如a[2] = 4,说明2行4列(0行0列为最开始的行列号)
 5     bool place(int *a, int n, int row, int col)    // 判断第row行第col列可否放置皇后
 6     {
 7         for (int i = 0; i < n; ++i)         // 逐行搜索有无冲突,i代表i行
 8         {
 9             if (a[i] == col || abs(i - row) == abs(a[i] - col))
10                 return 0;
11         }
12         return 1;
13     }
14
15     int totalNQueens(int n) {
16         int a[n];
17         for (int i =0; i < n; ++i)      // 数组初始化
18             a[i] = INITIAL;
19         int row = 0, col = 0;
20         int sum = 0;        // 有效的N皇后解
21
22         while (row < n)
23         {
24             while (col < n)     // 对第row行的每一列进行探测,看能否放置皇后
25             {
26                 if (place(a, n, row, col))
27                 {
28                     a[row] = col;
29                     col = 0;
30                     break;      // 此处break表示第row行已经放置好皇后,可以进入到下一行进行搜索
31                 }
32                 else
33                     ++col;
34             }
35             if (a[row] == INITIAL)      // 此时表示第row行没有找到可以放置皇后的位置
36             {
37                 if (row == 0)     // 回溯的终止条件
38                     break;
39                 else
40                 {
41                     --row;
42                     col = a[row] + 1;       // 把上一行的皇后往后移一列(还需判断后移后是否满足条件)
43                     a[row] = INITIAL;
44                     continue;
45                 }
46             }
47             if (row == n - 1)       // 程序能进入此判断内,则说明最后一行找到了皇后的位置,产生一个有效解
48             {
49                 ++sum;
50                 col = a[row] + 1;   // 把这个有效解的位置清除掉,从最后一行、后移一列的位置继续探测
51                 a[row] = INITIAL;
52                 continue;
53             }
54             ++row;
55         }
56         return sum;
57     }
58 };

中间犯了些小问题,这个题目最好在纸上把逻辑写下


9 / 9 test cases passed.
Status:

Accepted


Runtime: 6 ms

Submitted: 0 minutes ago
时间: 2024-11-05 18:28:59

【LeetCode】52.N-Queens II的相关文章

【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? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【LeetCode】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 ≤ lengt

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-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? 思路: 要求空间复杂度为O

【leetcode】Pascal&#39;s Triangle II (python)

其实每一行的结果是二项式展开的系数,但是考虑到当给定的参数过大的时候,在求组合的过程中会出现溢出(中间过程要用到乘法),但是这样的算法的时间复杂度是O(N),所以在参数不太大的时候,还是不错的. 这里用迭代的方法来求,当然复杂度就高了,是O(N^2),这里主要说下迭代时候的技巧,即在一个列表(数组)里进行迭代,实现如此的操作,要求在求下一行的时候,要从后往前进行,若是从前向后,就把后面要用的变量给改掉了,产生"脏"数据.从后向前不会(为何?),因为下一行比上一行多一个.(自己写写例子看

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium 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

【LeetCode】Single Number I &amp; II

Single Number I : Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Solution: 解法不少,贴一种: 1 cla

【LeetCode】House Robber I &amp; II 解题报告

[题目] I You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it wil

【LeetCode】Combination Sum I &amp; II 解题报告

[Combination Sum I] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

【LeetCode】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