N-Queens II

https://leetcode.com/problems/n-queens-ii/

Follow up for N-Queens problem.

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

解题思路:

这题和N-Queens相比,有啥简单的方法?没看出,无非就是算出所有解后返回结果的size。当然可能也可以维护一个值,有结果就递增,最后返回他就可以了。

public class Solution {

    public int totalNQueens(int n) {

        List<String[]> result = new ArrayList<String[]>();

        if(n < 1) {

            return 0;

        }

        String[] current = new String[n];

        for(int i = 0; i < n; i++) {

            StringBuffer bf = new StringBuffer();

            for(int j = 0; j < n; j++) {

                bf.append('.');

            }

            current[i] = bf.toString();

        }

        int[] columnInRow = new int[n];

        for(int i = 0; i < n; i++) {

            columnInRow[i] = -1;

        }

        dfs(n, result, current, columnInRow, 0);

        return result.size();

    }

    

    public void dfs(int n, List<String[]> result, String[] current, int[] columnInRow, int row) {

        if(row == n) {

            String[] temp = Arrays.copyOf(current, current.length);

            result.add(temp);

            return;

        }

        for(int i = 0; i < n; i++) {

            if(checkValid(columnInRow, row, i)) {

                columnInRow[row] = i;

                String temp = current[row];

                current[row] = current[row].substring(0, i) + "Q" + current[row].substring(i + 1);

                dfs(n, result, current, columnInRow, row + 1);

                current[row] = temp;

                columnInRow[row] = -1;

            }

        }

    }

    

    public boolean checkValid(int[] columnInRow, int row, int column) {

        int temp = row - 1, i = 1;

        while(temp >= 0) {

            if(columnInRow[temp] == column) {

                return false;

            }

            if(column - i >= 0 && columnInRow[temp] == column - i) {

                return false;

            }

            if(column + i < columnInRow.length && columnInRow[temp] == column + i) {

                return false;

            }

            i++;

            temp--;

        }

        

        temp = row + 1;

        i = 1;

        while(temp < columnInRow.length) {

            if(columnInRow[temp] == column) {

                return false;

            }

            if(column - i >= 0 && columnInRow[temp] == column - i) {

                return false;

            }

            if(column + i < columnInRow.length && columnInRow[temp] == column + i) {

                return false;

            }

            i++;

            temp++;

        }

        return true;

    }

}

又优化了一下省去了构造string的过程,并直接返回这个size。

public class Solution {

    public int totalNQueens(int n) {

        if(n < 1) {

            return 0;

        }

        int[] columnInRow = new int[n];

        for(int i = 0; i < n; i++) {

            columnInRow[i] = -1;

        }

        int result = 0;

        result = dfs(n, columnInRow, 0, result);

        return result;

    }

    

    public int dfs(int n, int[] columnInRow, int row, int result) {

        if(row == n) {

            result++;

            return result;

        }

        for(int i = 0; i < n; i++) {

            if(checkValid(columnInRow, row, i)) {

                columnInRow[row] = i;

                result = dfs(n, columnInRow, row + 1, result);

                columnInRow[row] = -1;

            }

        }

        return result;

    }

    

    public boolean checkValid(int[] columnInRow, int row, int column) {

        int temp = row - 1, i = 1;

        while(temp >= 0) {

            if(columnInRow[temp] == column) {

                return false;

            }

            if(column - i >= 0 && columnInRow[temp] == column - i) {

                return false;

            }

            if(column + i < columnInRow.length && columnInRow[temp] == column + i) {

                return false;

            }

            i++;

            temp--;

        }

        

        temp = row + 1;

        i = 1;

        while(temp < columnInRow.length) {

            if(columnInRow[temp] == column) {

                return false;

            }

            if(column - i >= 0 && columnInRow[temp] == column - i) {

                return false;

            }

            if(column + i < columnInRow.length && columnInRow[temp] == column + i) {

                return false;

            }

            i++;

            temp++;

        }

        return true;

    }

}

 

你可能感兴趣的:(UE)