22. 括号生成
数字 n
代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例 1:
输入:n = 3 输出:["((()))","(()())","(())()","()(())","()()()"]
示例 2:
输入:n = 1 输出:["()"]
提示:
1 <= n <= 8
class Solution {
public List<String> generateParenthesis(int n) {
List<String> list=new ArrayList<>();
String str="";
fangfa(str,n,0,0,list);
return list;
}
public void fangfa(String str,int n,int lcount,int rcount,List<String> list){
if (lcount==n&&rcount==n){
list.add(str);
return;
}
if(lcount<rcount||lcount>n||rcount>n) return;
//左
fangfa(str+"(",n,lcount+1,rcount,list);
//右
fangfa(str+")",n,lcount,rcount+1,list);
}
}
46. 全排列
给定一个不含重复数字的数组 nums
,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3] 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1] 输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1] 输出:[[1]]
提示:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums
中的所有整数 互不相同
class Solution {
List<List<Integer>> list=new ArrayList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
b(0,nums);
return list;
}
public void b(int t,int[] x){
if(t>=x.length){
List<Integer> l=new ArrayList<>();
for (int i = 0; i < x.length; i++) {
l.add(x[i]);
}
list.add(l);
}
else {
for (int i = t; i < x.length; i++) {
// swap(x[t],x[i]);
int temp=x[t];
x[t]=x[i];
x[i]=temp;
b(t+1,x);
// swap(x[t],x[i]);
temp=x[t];
x[t]=x[i];
x[i]=temp;
}
}
}
}
无剪枝条件、递归
51. N 皇后
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n
个皇后放置在 n×n
的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n
,返回所有不同的 n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q'
和 '.'
分别代表了皇后和空位。
示例 1:
输入:n = 4 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 解释:如上图所示,4 皇后问题存在两个不同的解法。
示例 2:
输入:n = 1 输出:[["Q"]]
提示:
1 <= n <= 9
class Solution {
static int n;
static int sum=0;
static List<List<String>> List=new ArrayList<>();
public List<List<String>> solveNQueens(int m) {
List.clear();
n=m;
int x[]=new int [n+1];
Backtrack(1,x);
System.out.println(sum);
return List;
}
public static int[] Backtrack(int t,int x[]){
if(t>n){
sum++;
String l="";
List<String> list=new ArrayList<>();
for (int i = 1; i < x.length; i++) {
l="";
for (int j = 1; j < x.length; j++) {
if(j==x[i]) l+="Q";
else l+=".";
}
list.add(l);
}
List.add(list);
}
else{
for (int i = 1; i <= n; i++) {
x[t] = i;
if(Place(t,x)){
Backtrack(t+1,x);
}
}
}
return x;
}
public static boolean Place(int t,int[] x){
for (int j = 1; j <= t-1; j++) {
if((Math.abs(t - j) == Math.abs(x[j] - x[t]))||(x[j] - x[t]==0)){
return false;
}
}
return true;
}
}
52. N 皇后 II
n 皇后问题 研究的是如何将 n
个皇后放置在 n × n
的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n
,返回 n 皇后问题 不同的解决方案的数量。
示例 1:
输入:n = 4 输出:2 解释:如上图所示,4 皇后问题存在两个不同的解法。
示例 2:
输入:n = 1 输出:1
提示:
1 <= n <= 9
class Solution {
static int n;
static int sum;
public int totalNQueens(int m) {
sum=0;
n=m;
int x[]=new int [n+1];
Backtrack(1,x);
return sum;
}
public static int[] Backtrack(int t,int x[]){
if(t>n){
sum++;
}
else{
for (int i = 1; i <= n; i++) {
x[t] = i;
if(Place(t,x)){
Backtrack(t+1,x);
}
}
}
return x;
}
public static boolean Place(int t,int[] x){
for (int j = 1; j <= t-1; j++) {
if((Math.abs(t - j) == Math.abs(x[j] - x[t]))||(x[j] - x[t]==0)){
return false;
}
}
return true;
}
}
借用上面N皇后的算法,同样的思路,N皇后II比N皇后简单一点。