Java実装-Nクイーンズ問題2
771 ワード
nクイーンズの問題に基づいて、具体的な配置レイアウトではなく、nクイーンズの異なる解決策の数を返します.
実際の面接でこの問題に遭遇したことがありますか?
Yes
サンプル
例えばn=4で2つのソリューションが存在する
実際の面接でこの問題に遭遇したことがありますか?
Yes
サンプル
例えばn=4で2つのソリューションが存在する
class Solution {
/**
* Calculate the total number of distinct N-Queen solutions.
* @param n: The number of queens.
* @return: The total number of distinct solutions.
*/
public int totalNQueens(int n) {
//write your code here
if(n<1){
return 0;
}
int []record=new int[n];
return process1(0,record,n);
}
private static int process1(int i,int []record,int n){
if(i==n){
return 1;
}
int res=0;
for(int j=0;j