# N-QueensII : 4ms and short

class Solution {
public:
void helper(int row, int &res, int n, vector<int>& solution) {
if (row == n+1) {
++ res;
}
for (int i = 1; i <= n; ++ i) {
if (canPlace(row, i, solution)) {
solution[row-1] = i;
helper(row+1, res, n, solution);
solution[row-1] = -1;
}
}
}

bool canPlace(int row, int i, vector<int>& solution) {
for (int j = 1; j < row; ++ j) {
if (i == solution[j-1] || abs(i-solution[j-1]) == abs(row-j))
return false;
}
return true;
}

int totalNQueens(int n) {
vector<int> solution(n, -1);
int res = 0;
if (n < 1)
return res;
helper(1, res, n, solution);
return res;
}
};

• 本文已收录于以下专栏：

## 2-sum, 3-sum, 4-sum and n-sum

2-sum Q: To find the composition of two number in a given array arr, that their sum is k which is a...

## CS231n系列课程Lecture4:Backpropagation and Neural Networks(part 1)

• Felaim
• 2017年04月12日 10:37
• 314

## Short-time Energy and Zero Crossing Rate

• 2014年06月23日 12:51
• 59KB
• 下载

## Day Trading With Short Term Price Patterns and Opening Range Breakout.

• 2013年09月09日 11:48
• 20.22MB
• 下载

## htons函数详解:网络与主机字节转换函数:(s 就是short l是long h是host n是network)

http://blog.csdn.net/libuding/article/details/5860578   Part 1: htons函数具体解释      在Linux和Windows网络...

## American Political Parties and Elections - A Very Short Introduction

• 2013年08月26日 14:47
• 2.23MB
• 下载

## Short and Stateless Signatures from the RSA Assumption

• 2012年05月17日 17:30
• 232KB
• 下载