题目:

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. java 将class打包成jar包

    一.进入到java jdk安装目录(用cmd命令进入) c:cd C:\Program Files\Java\jdk1.8.0_45\bin 二.运行打包命令(将D盘下com中的class 打包成xy ...

  2. 跟厂长学PHP7内核(三):源码目录结构

    上篇文章我们已经介绍了源码分析工具的安装.配置以及调试方法,本文我们来讲述一下PHP源码的目录结构. 一.目录概览 以php-7.0.12为例,看过源码的同学们应该发现源码目录多达十多个,下面是每个目 ...

  3. Sockets CF732E set map

    题目:http://codeforces.com/problemset/problem/732/E 题目大意: 有n台电脑(computer)和m个插座(socket),两者均有一个power值,分别 ...

  4. 把eclipse写好的web项目导入idea 部署到Tomcat

    主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此例中选 ...

  5. BZOJ 3238 [Ahoi2013]差异(后缀自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个串,设T[i]表示从第i位开始的后缀, 求sum(len( ...

  6. 20162325 金立清 S2 W11 C20

    20162325 2017-2018-2 <程序设计与数据结构>第11周学习总结 教材关键概念摘要 在哈希方法中,元素保存在哈希表中,其在表中的位置由哈希函数确定. 两个元素或关键字映射到 ...

  7. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. 你了解for循环吗

    大家学什么语言都会学for循环 可是你真的会用吗 通常写法都是 var arr=arr[1,2,3,4]; for(var i=0;i<arr.length;i++){ console.log( ...

  9. PIC JDM Prototype Programmer 1001

    In need of a programmer for PIC micro controllers I decided to build my own one. This programmer has ...

  10. MC34063A development aid

    http://www.nomad.ee/micros/mc34063a/index.shtml This is a simple-minded design tool that allows you ...