题目:

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. 基于五阶段流水线的RISC-V CPU模拟器实现

    RISC-V是源自Berkeley的开源体系结构和指令集标准.这个模拟器实现的是RISC-V Specification 2.2中所规定RV64I指令集,基于标准的五阶段流水线,并且实现了分支预测模块 ...

  2. shell rename directory

    mv can do two jobs. It can move files or directories It can rename files or directories To just rena ...

  3. C语言初始化

    注意:为什么要进行C语言环境的初始化?在没有进行C语言环境的初始化之前的初始化工作都是用汇编进行初始化的.比如核心初始化,和内存初始化 栈:栈帧:一个进程中一般会有多个函数,每一个函数都需要在内存中开 ...

  4. Spring之Spring环境搭建

    Spring之Spring环境搭建 一.什么是Spring? Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spr ...

  5. hdu 4548 初始化+二分 *

    题意:小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身 ...

  6. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  7. SPOJ 10232. Distinct Primes

    Arithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger i ...

  8. python循环与判断

    学习一门新的语言最重要的就是练习. 一.脚本需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 二.脚本流程图: 写代码之前画个流程图总是好的,可以让你理清思路,避免写着写着 ...

  9. Go语言Web框架gwk介绍 (一)

    今天看到Golang排名到前30名了,看来关注的人越来越多了,接下来几天详细介绍Golang一个web开发框架GWK. 现在博客园支持markdown格式发布文章么?后台的编辑器不太好用嘛. GWK ...

  10. EJB (Enterprise Java Bean) 理解

    做开发有段时间了,一直似懂非懂的. http://blog.csdn.net/jojo52013145/article/details/5783677