52. N-Queens II

= =这个题好像跟另一个N-QUEEN一样的。。
我还以为distinct要转棋盘呢。。比如某种解旋转期盼180°和另一种一样,就要去掉。。结果发现根本不是,就是算总数,比上一个少了个构建LIST的步骤。

public class Solution 
{
    int res = 0;
    public int totalNQueens(int n) 
    {
        int[] dp = new int[n];
        
        helper(dp,n,0);
    
        return res;    
    }
    
    public void helper(int[] dp, int n, int row)
    {
        if(n == row) res++;
        else
        {
            for(int i = 0; i < n;i++)
            {
                dp[row] = i;
                if(isFine(dp,row)) helper(dp,n,row+1);
            }
        }
    }
    
    
    public boolean isFine(int[] dp, int atWhere)
    {
        for(int i = 0; i < atWhere;i++)
        {
            if(dp[i] == dp[atWhere] || Math.abs(i-atWhere) == Math.abs(dp[i] - dp[atWhere])) return false;
            
        }
        return true;
    }
    
    
}

目前位置最简单的H题。。

原文地址:https://www.cnblogs.com/reboot329/p/5888177.html