Follow up for N-Queens problem.

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

解题思路:和上题一样《JAVA语言程序设计》中exerice6-22已经给出了计算count的代码,直接拿来用即可,JAVA实现如下:

static public int totalNQueens(int n) {
if(n==1)
return 1;
int count = 0;
int[] queens = new int[n]; // queens are placed at (i, queens[i])
for (int i = 0; i < n; i++)
queens[i] = -1;
queens[0] = 0;
int k = 1;
while (k >=0) {
int j = findPosition(k, queens,n);
if (j ==-1) {
queens[k] = -1;
k--; // back track to the previous row
} else {
queens[k] = j;
if (k == n-1)
count++;
else {
k++;
}
}
}
return count;
}
public static int findPosition(int k, int[] queens,int n) {
int start = queens[k] == -1 ? 0 : queens[k] + 1;
for (int j = start; j < n; j++) {
if (isValid(k, j, queens,n))
return j;
}
return -1;
} public static boolean isValid(int k, int j, int queens[],int n) {
for (int i = 0; i < k; i++)
if (queens[i] == j)
return false;
for (int row = k - 1, column = j - 1; row >= 0 && column >= 0; row--, column--)
if (queens[row] == column)
return false;
for (int row = k - 1, column = j + 1; row >= 0 && column <= n-1; row--, column++)
if (queens[row] == column)
return false;
return true;
}

Java for LeetCode 052 N-Queens II的更多相关文章

  1. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  2. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. Java for LeetCode 126 Word Ladder II 【HARD】

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

  5. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

  6. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  7. Java for LeetCode 229 Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. Java for LeetCode 219 Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  9. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

随机推荐

  1. 关于bash的shellshock漏洞

    这一漏洞的描述如下: Shellshock (CVE-2014-6271, CVE-2014-6277, CVE-2014-6278, CVE-2014-7169, CVE-2014-7186, CV ...

  2. IOS基础之 (一) OC基础语法

    一 OC语法 1.关键字 基本上所有关键字都是以@开头,比如: @interface , @implementation, @end, @public, @protected, @private 2. ...

  3. linux c学习笔记----进程创建(fork,wait,waitpid)

    1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...

  4. 漂亮的title提示信息

    <HTML> <HEAD> <title>一种很酷的文字提示效果演示</title> <style> .tableBorder7{width ...

  5. C++ 中map 中迭代器的简单使用:

    public member function <map> std::map::find iterator find (const key_type& k); const_itera ...

  6. 我的电脑右下角的日期也不见了只剩下时间,Win7系统,请问是什么原因啊?

    A:今天是2013/10/10,日期变为八位,宽度就不够了,把任务栏拉宽就好了 Q:win7 任务栏时间区可以拉宽吗?使之显示日期等 A:你的任务栏锁定了的,右键单击任务栏-锁定任务栏前面的勾去掉,鼠 ...

  7. [转载]AngularJS and scope.$apply

    http://jimhoskins.com/2012/12/17/angularjs-and-apply.html http://www.cnblogs.com/zhrj000/p/3383898.h ...

  8. (转)也谈基于NodeJS的全栈式开发(基于NodeJS的前后端分离)

    原文链接:http://ued.taobao.org/blog/2014/04/full-stack-development-with-nodejs/ 随着不同终端(pad/mobile/pc)的兴起 ...

  9. excel插入当前时间快捷键Ctrl+;

    之前写了一篇editplus如何插入当前时间_Ctrl+D的文章,有的同学说excel用习惯了,那在这我们就说一下excel插入当前时间快捷键,让您在excel快速插入当前时间 excel插入当前时间 ...

  10. 什么是A记录?MX记录?CNAME记录?它们都有些什么用途?

    什么是A记录?什么是MX记录?CNAME记录又是什么?它们都有些什么用途? 好,下面就用我浅陋经验给大家介绍一下: 1. A记录:WEB服务器的IP指向 A (Address) 记录是用来指定主机名( ...