题目:

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

题解:

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

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

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

代码如下:

 1     public int totalNQueens(int n) {  
 2         int[] res = {0};
 3         if(n<=0)
 4             return res[0];
 5             
 6         int [] columnVal = new int[n];
 7         
 8         DFS_helper(n,res,0,columnVal);
 9         return res[0];
     }
     
     public void DFS_helper(int nQueens, int[] res, int row, int[] columnVal){
         if(row == nQueens){
             res[0] += 1;
         }else{
             for(int i = 0; i < nQueens; i++){
                 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
                 
                 if(isValid(row,columnVal))
                     DFS_helper(nQueens, res, row+1, columnVal);
             }
         }
     }
     
     public boolean isValid(int row, int [] columnVal){
         for(int i = 0; i < row; i++){
             if(columnVal[row] == columnVal[i]
                ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
                return false;
         }
         return true;

使用全局变量来记录结果的代码是:

 1     int res;
 2     public int totalNQueens(int n) { 
 3         res = 0;
 4         if(n<=0)
 5             return res;
 6             
 7         int [] columnVal = new int[n];
 8         
 9         DFS_helper(n,0,columnVal);
         return res;
     }
     
     public void DFS_helper(int nQueens, int row, int[] columnVal){
         if(row == nQueens){
             res += 1;
         }else{
             for(int i = 0; i < nQueens; i++){
                 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
                 
                 if(isValid(row,columnVal))
                     DFS_helper(nQueens, row+1, columnVal);
             }
         }
     }
     
     public boolean isValid(int row, int [] columnVal){
         for(int i = 0; i < row; i++){
             if(columnVal[row] == columnVal[i]
                ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
                return false;
         }
         return true;
     }

N-Queens II leetcode java的更多相关文章

  1. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  2. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  3. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  4. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

  5. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  6. Ugly Number II leetcode java

    问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...

  7. Word Ladder II leetcode java

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. Binary Tree Level Order Traversal II leetcode java

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  9. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

随机推荐

  1. CSUOJ 1040 Round-number

    Description Most of the time when rounding a given number, it is customary to round to some multiple ...

  2. 用户管理和FTP服务配置

    批量创建用户 python脚本:creuser.py import osulist=open('usernames','r')for x in ulist: cmd="useradd -g ...

  3. BZOJ 3091: 城市旅行 lct 期望 splay

    https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...

  4. [CC-SEINC]Sereja and Subsegment Increasings

    [CC-SEINC]Sereja and Subsegment Increasings 题目大意: 有长度为\(n(n\le10^5)\)的序列\(A\)和\(B\). 在一次操作中,可以选择一个区间 ...

  5. [POJ2337]Catenyms

    题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...

  6. 安装第三方jar包的两种方式

    由于部分第三放jar包没有放到maven中央仓库,而项目中又依赖了这些jar包,那么如何安装?我实践了两种,特做记录. 一.安装到 nexus 私有库: 在 3rd party 仓库下有个 Artif ...

  7. SVN 提交回滚

    取消对代码的修改分为两种情况:   第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [ ...

  8. PPT文化

    PPT文化,yes or no? 知识是有体系的,有的时候刚接触的时候可以 推导技术 ,汇报.吹牛都可以应用上,并且可以让别人想想. 但是实际应用技术,就需要涉及很多详细的技术细节,如果少掉一个看似极 ...

  9. [置顶] Spring的自动装配

    采用构造函数注入,以及setter方法注入都需要写大量的XML配置文件,这时可以采用另一种方式,就是自动装,由Spring来给我们自动装配我们的Bean. Spring提供了四种自动装配类型 1:By ...

  10. Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)

    Cocos2d—X游戏开发之CCToggle(菜单标签切换) 首先继承子CCMenu,是菜单标签中的一种.‘ class CC_DLL CCMenuItemToggle : public CCMenu ...