[LeetCode] NQueen II

public class Solution {
    public List<String[]> solveNQueens(int n) {
        int [] locations;
        locations = new int[n];
        List<String[]> result = new ArrayList<String[]>();

        getNQueens(locations, 0, result);
        return result;
    }

    public void getNQueens(int [] locations, int row, List<String[]> solutions) {
        if (row == locations.length) {
            String [] solution = new String[row];
            for(int i=0; i<locations.length; i++) {
                StringBuffer tmpRow = new StringBuffer();
                for (int j=0; j<locations.length; j++) {
                    if(j == locations[i]) tmpRow.append("Q");
                    else tmpRow.append(".");
                }
                solution[i] = tmpRow.toString();
            }
            solutions.add(solution);
        }

        for (int i=0; i<locations.length; i++) {
            if (check(locations, row, i)) {
                locations[row] = i;
                getNQueens(locations, row+1, solutions);
            }
        }
    }

    public boolean check(int [] locations, int row, int clom) {
        for (int i=0; i<row; i++) {
            if (locations[i] == clom) return false;
            if (locations[i]+i == row+clom) return false;
            if (i-locations[i] == row-clom) return false;
        }

        return true;
    }
}
时间: 2024-10-10 10:13:39

[LeetCode] NQueen II的相关文章

[leetcode]Subsets II @ Python

原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicat

LeetCode: N-Queens II [051]

[题目] Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. [题意] 解N皇后问题,N-Queens要求返回所有的解,而本题只需要返回可行解的数目 [思路] DFS,参考N-Queens [代码] class Solution { public: bool isValid(vector<string

LeetCode: Permutations II [046]

[题目] Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

LeetCode OJ--Permutations II

给的一个数列中,可能存在重复的数,比如 1 1  2 ,求其全排列. 记录上一个得出来的排列,看这个排列和上一个是否相同. #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: vector<vector<int> > permuteUnique(vector<int> &n

LeetCode: Subsets II [091]

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[LeetCode] Subsets II [32]

题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution

Leetcode: N-Queens II C++

class Solution { public: int totalNQueens(int n) { //initialize chessboard vector<vector<bool>> boardconf; vector<bool> orig_row; orig_row.assign(n,false); boardconf.assign(n,orig_row); int totalNum = 0; for(int j = 0; j < n; j++){ to

LeetCode: N-Queens II 解题报告

N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 解答: 与第一题的解法是类似的,不同之处是,DFS的返回值是解的个数.我们每一次搜索本行8个位置时, 把8个位置的解都加在一起就行了.有的位置无解,它的返回值就是0咯. 另外注意BASE

Leetcode: Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 难度:80+15(怎样跳过重复的case来节省时间).我一开始的想法很简单,就是在permutation这道题的