LeetCode – Refresh – N-Queens

Brute Force:

 1 class Solution {
 2 public:
 3     bool notExist(vector<int> rec, int level, int current) {
 4         for (int i = 0; i < level; i++) {
 5             if (rec[i] == current) return false;
 6             if (fabs(rec[i] - current) == fabs(level - i)) return false;
 7         }
 8         return true;
 9     }
10     void genQueen(vector<int> rec, vector<string> &result) {
11         for (int i = 0; i < rec.size(); i++) {
12             ostringstream oss;
13             for (int j = 0; j < rec.size(); j++) {
14                 if (rec[i] == j) oss << ‘Q‘;
15                 else oss << ‘.‘;
16             }
17             result.push_back(oss.str());
18         }
19     }
20     void getQueen(vector<vector<string> > &result, vector<int> rec, int n, int level) {
21         if (level == n) {
22             result.push_back(vector<string>());
23             genQueen(rec, result.back());
24             return;
25         }
26         for (int i = 0; i < n; i++) {
27             if (notExist(rec, level, i)) {
28                 rec[level] = i;
29                 getQueen(result, rec, n, level+1);
30             }
31         }
32     }
33     vector<vector<string> > solveNQueens(int n) {
34         vector<vector<string> > result;
35         getQueen(result, vector<int> (n,0), n, 0);
36         return result;
37     }
38 };
时间: 2024-10-11 20:59:33

LeetCode – Refresh – N-Queens的相关文章

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res

LeetCode – Refresh – Fraction to Recurring Decimal

Notes: 1. When numerator is 0, return "0". Check this corner case, because 0 / -5 will return -0. 2. Use long long int for divd and divs, mainly for divs. Because when divs = INT_MIN, fabs(divs) is large. 3. Do not forget to rest *= 10 to get th

LeetCode – Refresh – Implement strStr()

Brute Force: 1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 if (!haystack) return -1; 5 if (!needle) return 0; 6 int index = 0, tmp1 = 0, tmp2 = 0; 7 while (haystack[index]) { 8 if (haystack[index] == needle[0]) { 9 char

LeetCode – Refresh – Gas Station

Not quite hard. Just remember initialize index to 0. Because if you initialize it as -1 and all the gas satisfy the cost, it will return -1. Actually, it was 0. 1 class Solution { 2 public: 3 int canCompleteCircuit(vector<int> &gas, vector<in

LeetCode – Refresh – Gray Code

i ^ (i >> 1), that's the general format 1 class Solution { 2 public: 3 vector<int> grayCode(int n) { 4 vector<int> result; 5 for (int i = 0; i < (1 << n); i++) { 6 result.push_back(i ^ (i >> 1)); 7 } 8 return result; 9 } 1

LeetCode – Refresh – Maximum Depth of Binary Tree

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int maxDepth(TreeNode *root) { 13

LeetCode – Refresh –

1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 int result = A[0], sum = A[0]; 5 for (int i = 1; i < n; i++) { 6 sum = max(sum + A[i], A[i]); 7 result = max(result, sum); 8 } 9 return result; 10 } 11 };

LeetCode – Refresh – Median of Two Sorted Array

O(m + n): Note: return rec[1] not rec[0]. 1 class Solution { 2 public: 3 double findMedianSortedArrays(int A[], int m, int B[], int n) { 4 int mid = (m+n)/2 + 1, i = 0, j = 0; 5 vector<int> rec(2); 6 while (i + j < mid) { 7 int tmp1 = INT_MAX, tm