美文网首页Lintcode程序员
Lintcode34 N-Queens II solution

Lintcode34 N-Queens II solution

作者: 代码码着玩 | 来源:发表于2017-04-02 05:38 被阅读32次

【题目描述】

Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.

根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局。

【题目链接】

http://www.lintcode.com/en/problem/n-queens-ii/

【题目解析】

这道题跟NQueens的解法完全一样(具体解法参照N QueensN Queens leetcode java),只不过要求的返回值不同了。。所以要记录的result稍微改一下就好了。。。

因为涉及到递归,result传进去引用类型(List,数组之类的)才能在层层递归中得以保存,所以这里使用一个长度为1的数组帮助计数。

当然,也可以使用一个全局变量来帮助计数。

【参考答案】

http://www.jiuzhang.com/solutions/n-queens-ii/

相关文章

网友评论

    本文标题:Lintcode34 N-Queens II solution

    本文链接:https://www.haomeiwen.com/subject/dbjvnttx.html