leetcode N-Queens I && N-Queens II

第一个的代码:

 1 #include<iostream>
 2 #include<vector>
 3
 4 using namespace std;
 5
 6 bool isLegal(int i, int j, vector<string> &current)
 7 {
 8     int size = current.size();
 9     int x = i-1, y = j;
10     while (x >= 0)
11     {
12         if (current[x][y] == ‘Q‘)
13             return false;
14         x--;
15     }
16     x = i-1;
17     y = j - 1;
18     while (x >= 0 && y >= 0)
19     {
20         if (current[x][y] == ‘Q‘)
21             return false;
22         x--;
23         y--;
24     }
25     x = i - 1;
26     y = j + 1;
27     while (x >= 0 && y < size)
28     {
29         if (current[x][y] == ‘Q‘)
30             return false;
31         x--;
32         y++;
33     }
34     return true;
35 }
36
37 void getResult(int row, int index, vector<vector<string>> &result, vector<string> &current, int size)
38 {
39     if (row == size)
40         result.push_back(current);
41     else
42     {
43         while (index < size)
44         {
45             current[row][index] = ‘Q‘;
46             if (isLegal(row, index, current))
47                 getResult(row + 1, 0, result, current, size);
48             current[row][index] = ‘.‘;
49             index++;
50         }
51     }
52 }
53
54 vector<vector<string>> solveNQueens(int n)
55 {
56     vector<vector<string>> result;
57     string s = "";
58     for (int i = 0; i < n; i++)
59         s.push_back(‘.‘);
60     vector<string> current(n, s);
61     getResult(0, 0, result, current, n);
62     return result;
63 }
64
65 int main()
66 {
67     vector<vector<string>> result = solveNQueens(4);
68     for (int i = 0; i < result.size(); i++)
69     {
70         for (int j = 0; j < result[i].size(); j++)
71             cout << result[i][j].c_str() << endl;
72         cout << "-_________________________________________-" << endl;
73     }
74     return 0;
75 }

第二个的代码:
得,忘了保存了,算了,不贴了,这题也就做到这份上了,讨论区里那个用位的我真是佩服死你啦。。。。。!!!!!!!!!

时间: 2024-10-12 08:22:51

leetcode N-Queens I && N-Queens II的相关文章

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: 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-&g

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

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] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted List II [083]

[题目] 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-

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: 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

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro

leetcode第一刷_Linked List Cycle II

这道题稍微有点意思,知道答案发现,呀,这么简单就能做啊.我一开始想的是,相遇之后用另一个指针怎么走,然后满足什么关系之后能推出来,其实不用这么麻烦.是很简单的数学关系,我画个图说一下. S1代表的是链表进入环之前的长度,a代表当两个指针相遇时,走一步的指针在环里走的长度,S2代表的是环的周长,那么根据条件,相遇时,走两步的指针走的距离是走一步的两倍,我们得到公式: (S1+a)*2 = S1+S2+a 化简一下得到 S1 = S2-a 即,环中剩下的长度刚好等于进入链表之前的长度.于是解法是:当

【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]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){