给定两个整数 n
和 k
,返回范围 [1, n]
中所有可能的 k
个数的组合。
你可以按 任何顺序 返回答案。
思路:首先组合问题,就是结果中不能出现重复,然后我们要用一个startIndex来移动每一个集合里的数字,比如第一层1,2,3.。。。n,第二层2,3,4.。。n,此时startIndex=1,然后一次移动到n去不同的值。startIndex就是用来去重的。
示例 1:
输入:n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
示例 2:
输入:n = 1, k = 1
输出:[[1]]
class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
backTrack(n,k,1);
return res;
}
public void backTrack(int n,int k,int startIndex){
if(path.size()==k){
res.add(new ArrayList<>(path));
return;
}
for(int i=startIndex;i<=n-(k-path.size())+1;i++){//n-(k-path.size())+1剪枝
path.add(i);
backTrack(n,k,i+1);
path.removeLast();
}
}
}
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backTrack(candidates,0,target);
return res;
}
public void backTrack(int[] candidates,int startIndex,int target){
if(target==0){
res.add(new ArrayList<>(path));
return;
}else if(target<0){
return;
}
for(int i=startIndex;i<candidates.length;i++){
path.add(candidates[i]);
backTrack(candidates,i,target-candidates[i]);
path.removeLast();
}
}
}
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTrack(candidates,0,target);
return res;
}
public void backTrack(int[] candidates,int startIndex,int target){
if(target==0){
res.add(new ArrayList<>(path));
return;
}else if(target<0){
return;
}
for(int i=startIndex;i<candidates.length;i++){
if(startIndex<i && candidates[i]==candidates[i-1]){
continue;
}
path.add(candidates[i]);
backTrack(candidates,i+1,target-candidates[i]);
path.removeLast();
}
}
}
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
说明:
- 所有数字都是正整数。
- 解集不能包含重复的组合。
示例 1: 输入: k = 3, n = 7 输出: [[1,2,4]]
示例 2: 输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]]
class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(k,n,1);
return res;
}
public void backTrack(int k,int n,int startIndex){
if(path.size()==k && n==0){//收集到k个元素并且集合中元素和为n
res.add(new ArrayList<>(path));
return;
}
for(int i=startIndex;i<=9;i++){
path.add(i);
backTrack(k,n-i,i+1);
path.removeLast();
}
}
}
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
class Solution {
//设置全局列表存储最后的结果
List<String> res= new ArrayList<>();
StringBuilder sb=new StringBuilder();
public List<String> letterCombinations(String digits) {
if(digits.length()==0) return res;
String[] s={" "," ","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
backTrack(digits,s,0);
return res;
}
public void backTrack(String digits,String[] s,int num){
if(num==digits.length()){
res.add(sb.toString());
return;
}
String str=s[digits.charAt(num)-'0'];
for(int i=0;i<str.length();i++){
sb.append(str.charAt(i));
backTrack(digits,s,num+1);
sb.deleteCharAt(sb.length()-1);
}
}
}
组合总和,组合总和II,组合总和III,电话号码组合总结:组合问题注意这几点:1.确定是组合问题,也就是说元素中不能有重复的 2.是否有startIndex,如果每次取的数都是一个集合取出的那么要startIndext;如果多个集合取数据那么不需要startIndex,比如电话号码数,第一个abc,第二个取def 3.能否重复选取元素,如果能那么就是i,不能就是i+1;4.看所给数组(原数据)是否含重复的,如果含重复需要去重,去重一定要先排序!如果不含重复的则不需要去重。
给你一个字符串 s
,请你将 s
分割成一些子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]
class Solution {
List<List<String>> res = new ArrayList<>();
LinkedList<String> path = new LinkedList<>();
public List<List<String>> partition(String s) {
backTrack(s,0);
return res;
}
public void backTrack(String s,int startIndex){
if(startIndex>=s.length()){
res.add(new ArrayList<>(path));
return;
}
for(int i=startIndex;i<s.length();i++){
if(isNum(s,startIndex,i)){
String str=s.substring(startIndex,i+1);
path.add(str);
}else{
continue;
}
backTrack(s,i+1);
path.removeLast();
}
}
public boolean isNum(String s,int start,int end){
while(start<end){
if(s.charAt(start)!=s.charAt(end)){
return false;
}
start++;
end--;
}
return true;
}
}
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。
示例 1:
- 输入:s = "25525511135"
- 输出:["255.255.11.135","255.255.111.35"]
示例 2:
- 输入:s = "0000"
- 输出:["0.0.0.0"]
示例 3:
- 输入:s = "1111"
- 输出:["1.1.1.1"]
示例 4:
- 输入:s = "010010"
- 输出:["0.10.0.10","0.100.1.0"]
示例 5:
- 输入:s = "101023"
- 输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
提示:
- 0 <= s.length <= 3000
- s 仅由数字组成
class Solution {
List<String> res= new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
backTrack(s,0,0);
return res;
}
public void backTrack(String s,int startIndex,int pointNum){
if(pointNum==3){
if(isVaild(s,startIndex,s.length()-1)){
res.add(s);
}
return;
}
for(int i=startIndex;i<s.length();i++){
if(isVaild(s,startIndex,i)){
s=s.substring(0,i+1)+"."+s.substring(i+1);
pointNum++;
backTrack(s,i+2,pointNum);
pointNum--;
s=s.substring(0,i+1)+s.substring(i+2);
}else{
break;
}
}
}
public boolean isVaild(String s,int start,int end){
if(start>end) return false;
if(s.charAt(start)=='0'&&start!=end) return false;
int num=0;
for(int i=start;i<=end;i++){
if(s.charAt(i)>9 || s.charAt(i)<0) return false;
num=num*10+s.charAt(start-'0');
if(num>255) return false;
}
return true;
}
}
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
public List<List<Integer>> subsets(int[] nums) {
if (nums.length == 0){
result.add(new ArrayList<>());
return result;
}
subsetsHelper(nums, 0);
return result;
}
private void subsetsHelper(int[] nums, int startIndex){
result.add(new ArrayList<>(path));//「遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合」。
if (startIndex >= nums.length){ //终止条件可不加
return;
}
for (int i = startIndex; i < nums.length; i++){
path.add(nums[i]);
subsetsHelper(nums, i + 1);
path.removeLast();
}
}
}
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例 1:
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
//这题重点在去重 去重要排序! 去重有两种方法第一是直接i > start && nums[i - 1] == nums[i]判断
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsetsWithDup( int[] nums ) {
Arrays.sort( nums );
subsetsWithDupHelper( nums, 0 );
return res;
}
private void subsetsWithDupHelper( int[] nums, int start ) {
res.add( new ArrayList<>( path ) );
for ( int i = start; i < nums.length; i++ ) {
// 跳过当前树层使用过的、相同的元素
if ( i > start && nums[i - 1] == nums[i] ) {
continue;
}
path.add( nums[i] );
subsetsWithDupHelper( nums, i + 1 );
path.removeLast();
}
}
}
//使用user数组判断
class Solution {
List<List<Integer>> res= new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
if (nums.length == 0){
result.add(path);
return res;
}
Arrays.sort(nums);
used = new boolean[nums.length];
subsetsWithDupHelper(nums, 0);
return result;
}
private void subsetsWithDupHelper(int[] nums, int startIndex){
res.add(new ArrayList<>(path));
for (int i = startIndex; i < nums.length; i++){
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
continue;
}
path.add(nums[i]);
used[i] = true;
subsetsWithDupHelper(nums, i + 1);
path.removeLast();
used[i] = false;
}
}
}
给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。
数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
//这题和前面去重方法不一样 因为它要找出递增序列 不能进行排序!path.isEmpty()&&nums[i]<path.get(path.size()-1) || user[nums[i]+100]==1
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTrack(nums,0);
return res;
}
public void backTrack(int[] nums,int startIndex){
if(path.size()>=2){
res.add(new ArrayList<>(path));
}
int[] user=new int[201];
for(int i=startIndex;i<nums.length;i++){
if(!path.isEmpty()&&nums[i]<path.get(path.size()-1) || user[nums[i]+100]==1){
continue;
}
user[nums[i]+100]=1;
path.add(nums[i]);
backTrack(nums,i+1);
path.removeLast();
}
}
}
给定一个不含重复数字的数组 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]]
class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
backTrack(nums);
return res;
}
public void backTrack(int[] nums){
if(path.size()==nums.length){
res.add(new ArrayList<>(path));
}
for(int i=0;i<nums.length;i++){
if(path.contains(nums[i])) continue;
path.add(nums[i]);
backTrack(nums);
path.removeLast();
}
}
}
class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
boolean[] user;
public List<List<Integer>> permuteUnique(int[] nums) {
user=new boolean[nums.length];
Arrays.sort(nums);
backTrack(nums);
return res;
}
public void backTrack(int[] nums){
if(path.size()==nums.length){
res.add(new ArrayList<>(path));
}
for(int i=0;i<nums.length;i++){
if(i>0&&nums[i]==nums[i-1]&&user[i-1]==false) continue;
if(user[i]==false){
user[i]=true;
path.add(nums[i]);
backTrack(nums);
path.removeLast();
user[i]=false;
}
}
}
}
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例 1:
输入:n = 4
输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释:如上图所示,4 皇后问题存在两个不同的解法。
示例 2:
输入:n = 1
输出:[["Q"]]
class Solution {
List<List<String>> res=new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
char[][] board=new char[n][n];
for(char[] c:board){
Arrays.fill(c,'.');
}
backTrack(n,0,board);
return res;
}
public void backTrack(int n,int row,char[][] board){
if(row==n){
res.add(ArrayList(board));
return;
}
for(int col=0;col<n;col++){
if(isVaild(row,col,n,board)){
board[row][col]='Q';
backTrack(n,row+1,board);
board[row][col]='.';
}
}
}
public List ArrayList(char[][] board){
List<String> list=new ArrayList<>();
for(char[] c:board){
list.add(String.copyValueOf(c));
}
return list;
}
public boolean isVaild(int row,int col,int n,char[][] board){
//列
for(int i=0;i<row;i++){
if(board[i][col]=='Q'){
return false;
}
}
//45°
for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){
if(board[i][j]=='Q'){
return false;
}
}
//135°
for(int i=row-1,j=col+1;i>=0&&j<n;i--,j++){
if(board[i][j]=='Q'){
return false;
}
}
return true;
}
}