题目:

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. NetworkX 使用(三)

    官方教程 博客:NetworkX NetworkX 使用(二) Introduction to Graph Analysis with NetworkX %pylab inline import ne ...

  2. Ubuntu下修改为永久DNS的方法

    安装好Ubuntu之后设置了静态IP地址,再重启后就无法解析域名.想重新设置一下DNS,打开/etc/resolv.conf cat /etc/resolv.conf # Dynamic resolv ...

  3. 【CF 585E】 E. Present for Vitalik the Philatelist

    E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...

  4. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  5. 利用meterpreter下的Venom免杀后门

    转载请注明作者:admin-神风 下载地址:https://github.com/r00t-3xp10it/venom .从Github上下载框架: tar.gz OR zip OR git clon ...

  6. [CodeChef-QTREE6]Query on a tree VI

    题目大意: 给你一棵黑白树,每个点默认是白色,要求支持以下两种操作: 1.改变一个点的颜色: 2.除去连接不同颜色的点的边,求某个点连通块的大小. 思路: 对原树维护两个树链剖分, 一棵维护当点x为白 ...

  7. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  8. php curl 发送get和post请求示例

    <?php final class HttpClient { const TIME_OUT = 10; static function get($url) { $ch = curl_init() ...

  9. Win8开机直接进桌面方法

    最新的Win8系统由于新增开始屏幕(UI)界面,专门为触摸设备准备,并且很多喜欢尝鲜的电脑爱好者朋友在我们传统的电脑上安装了Win8系统,不少PC用户开始都不喜欢Win8开机后进入UI界面而非传统的电 ...

  10. HDU 4716 A Computer Graphics Problem (水题)

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...