给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
示例:输入:n = 4, k = 2 输出: [ [1,2], [1,3], [1,4], [2,3], [2,4], [3,4]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> combine(int n, int k) {
- backTrack(n, k, 1);
- return list;
- }
-
- public void backTrack(int n, int k, int startIndex){
- if(path.size() == k){
- list.add(new ArrayList<>(path));
- return;
- }
- for(int i = startIndex; i <= n; i++){
- path.add(i);
- backTrack(n, k, i + 1);
- path.removeLast();
- }
- }
- }
找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:(1)只使用数字1到9;(2)每个数字 最多使用一次 。返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
示例:输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> combinationSum3(int k, int n) {
- backTrack(k, n, 1, 0);
- return list;
- }
-
- public void backTrack(int k, int targetSum, int startIndex, int sum){
- if(sum > targetSum || path.size() > k){
- return;
- }
- if(path.size() == k && sum == targetSum){
- list.add(new ArrayList<>(path));
- return;
- }
-
- for(int i = startIndex; i <= 9; i++){
- path.add(i);
- sum += i;
- backTrack(k, targetSum, i + 1, sum);
- path.removeLast();
- sum -= i;
- }
- }
- }
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
示例:输入:candidates = [2,3,6,7], target = 7 输出:[[2,2,3],[7]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> combinationSum(int[] candidates, int target) {
- Arrays.sort(candidates);
- backTrack(candidates, target, 0, 0);
- return list;
- }
-
- public void backTrack(int[] candidates, int target, int index, int sum){
- if(sum > target){
- return;
- }
- if(sum == target){
- list.add(new ArrayList<>(path));
- return;
- }
-
- for(int i = index; i < candidates.length; i++){
- path.add(candidates[i]);
- sum += candidates[i];
- backTrack(candidates, target, i, sum);
- sum -= candidates[i];
- path.removeLast();
- }
- }
- }
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用 一次 。
示例:输入: candidates = [10,1,2,7,6,1,5], target = 8 输出: [ [1,1,6], [1,2,5], [1,7], [2,6] ]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> combinationSum2(int[] candidates, int target) {
- Arrays.sort(candidates);
- backTrack(candidates, target, 0, 0);
- return list;
- }
-
- public void backTrack(int[] candidates, int target, int startIndex, int sum){
- if(sum == target){
- list.add(new ArrayList<>(path));
- return;
- }
-
- for(int i = startIndex; i < candidates.length; i++){
- if(sum + candidates[i] > target){
- break;
- }
- // 跳过同一层使用过的元素(必须在数组有序时才可以这样使用)
- if(i > startIndex && candidates[i] == candidates[i - 1]){
- continue;
- }
- path.add(candidates[i]);
- sum += candidates[i];
- backTrack(candidates, target, i + 1, sum);
- sum -= candidates[i];
- path.removeLast();
- }
- }
- }
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> combinationSum2(int[] candidates, int target) {
- Arrays.sort(candidates);
- back(candidates, target, 0, 0);
- return list;
- }
-
- public void back(int[] candidates, int target, int startIndex, int sum){
- if(sum > target){
- return;
- }
- if(sum == target){
- list.add(new ArrayList<>(path));
- return;
- }
-
- Set
set = new HashSet<>(); - for(int i = startIndex; i < candidates.length; i++){
- if(set.contains(candidates[i])){
- continue;
- }
- set.add(candidates[i]);
- path.add(candidates[i]);
- sum += candidates[i];
- back(candidates, target, i + 1, sum);
- sum -= candidates[i];
- path.removeLast();
- }
- }
- }
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:输入:digits = "23" 输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
- class Solution {
- List
list = new ArrayList<>(); - StringBuilder item = new StringBuilder();
-
- public List
letterCombinations(String digits) { - if(digits.length() == 0){
- return list;
- }
- String[] numberString = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- backTrack(digits, numberString, 0);
- return list;
- }
-
- public void backTrack(String digits, String[] numberString, int index){
- if(item.length() == digits.length()){
- list.add(item.toString());
- return;
- }
-
- String cur = numberString[digits.charAt(index) - '0'];
- for(int i = 0; i < cur.length(); i++){
- item.append(cur.charAt(i));
- backTrack(digits, numberString, index + 1);
- item.deleteCharAt(item.length() - 1);
- }
- }
- }
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
示例:输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
item = new LinkedList<>(); -
- public List
> partition(String s) {
- backTrack(s, 0);
- return list;
- }
-
- public void backTrack(String s, int startIndex){
- if(startIndex >= s.length()){
- list.add(new ArrayList<>(item));
- return;
- }
- for(int i = startIndex; i < s.length(); i++){
- if(!isPalindrome(s, startIndex, i)){
- continue;
- }
- item.add(s.substring(startIndex, i + 1));
- backTrack(s, i + 1);
- item.removeLast();
- }
- }
-
- public boolean isPalindrome(String s, int left, int right){
- while(left <= right){
- if(s.charAt(left) != s.charAt(right)){
- return false;
- }
- left++;
- right--;
- }
- return true;
- }
- }
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例:输入:nums = [1,2,3] 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> subsets(int[] nums) {
- backTrack(nums, 0);
- return list;
- }
-
- public void backTrack(int[] nums, int startIndex){
- list.add(new ArrayList<>(path));
- if(startIndex >= nums.length){ // 可以省略
- return;
- }
- for(int i = startIndex; i < nums.length; i++){
- path.add(nums[i]);
- backTrack(nums, i + 1);
- path.removeLast();
- }
- }
- }
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例:输入:nums = [1,2,2] 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> subsetsWithDup(int[] nums) {
- Arrays.sort(nums);
- backTrack(nums, 0);
- return list;
- }
-
- public void backTrack(int[] nums, int startIndex){
- list.add(new ArrayList<>(path));
- if(startIndex >= nums.length){ // 可以省略
- return;
- }
-
- for(int i = startIndex; i < nums.length; i++){
- if(i > startIndex && nums[i] == nums[i - 1]){
- continue;
- }
- path.add(nums[i]);
- backTrack(nums, i + 1);
- path.removeLast();
- }
- }
- }
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> subsetsWithDup(int[] nums) {
- Arrays.sort(nums);
- backTrack(nums, 0);
- return list;
- }
-
- public void backTrack(int[] nums, int startIndex){
- list.add(new ArrayList<>(path));
- if(startIndex >= nums.length){
- return;
- }
-
- Set
set = new HashSet<>(); - for(int i = startIndex; i < nums.length; i++){
- if(set.contains(nums[i])){
- continue;
- }
- set.add(nums[i]);
- path.add(nums[i]);
- backTrack(nums, i + 1);
- path.removeLast();
- }
- }
- }
给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例:输入: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]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> findSubsequences(int[] nums) {
- backTrack(nums, 0);
- return list;
- }
-
- public void backTrack(int[] nums, int startIndex){
- if(path.size() >= 2){
- list.add(new ArrayList<>(path));
- }
-
- // 由于数组无序,因此不能使用 if(i > startIndex && nums[i] == nums[i - 1])
- HashSet
set = new HashSet<>(); - for(int i = startIndex; i < nums.length; i++){
- if((!path.isEmpty() && nums[i] < path.getLast()) || set.contains(nums[i])){
- continue;
- }
- set.add(nums[i]);
- path.add(nums[i]);
- backTrack(nums, i + 1);
- path.removeLast();
- }
- }
- }
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例:输入:nums = [1,2,3] 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> permute(int[] nums) {
- boolean[] used = new boolean[nums.length];
- backTrack(nums, used);
- return list;
- }
-
- public void backTrack(int[] nums, boolean[] used){
- if(path.size() == nums.length){
- list.add(new ArrayList<>(path));
- return;
- }
-
- for(int i = 0; i < nums.length; i++){
- if(used[i]){
- continue;
- }
- used[i] = true;
- path.add(nums[i]);
- backTrack(nums, used);
- path.removeLast();
- used[i] = false;
- }
- }
- }
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例:输入:nums = [1,1,2] 输出: [[1,1,2], [1,2,1], [2,1,1]]
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> permuteUnique(int[] nums) {
- Arrays.sort(nums);
- boolean[] used = new boolean[nums.length];
- backTrack(nums, used);
- return list;
- }
-
- public void backTrack(int[] nums, boolean[] used){
- if(path.size() == nums.length){
- list.add(new ArrayList<>(path));
- return;
- }
-
- for(int i = 0; i < nums.length; i++){
- if(used[i] == true || (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false)){
- continue;
- }
- used[i] = true;
- path.add(nums[i]);
- backTrack(nums, used);
- path.removeLast();
- used[i] = false;
- }
- }
- }
- class Solution {
- List
> list = new ArrayList<>();
- LinkedList
path = new LinkedList<>(); -
- public List
> permuteUnique(int[] nums) {
- boolean[] used = new boolean[nums.length];
- backTrack(nums, used);
- return list;
- }
-
- public void backTrack(int[] nums, boolean[] used){
- if(path.size() == nums.length){
- list.add(new ArrayList<>(path));
- return;
- }
-
- Set
set = new HashSet<>(); - for(int i = 0; i < nums.length; i++){
- if(set.contains(nums[i]) || used[i] == true){
- continue;
- }
- set.add(nums[i]);
- used[i] = true;
- path.add(nums[i]);
- backTrack(nums, used);
- used[i] = false;
- path.removeLast();
- }
- }
- }
有效 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 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 '.' 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。
示例:输入:s = "25525511135" 输出:["255.255.11.135","255.255.111.35"]
- class Solution {
- List
list = new ArrayList<>(); -
- public List
restoreIpAddresses(String s) { - if(s.length() > 12){
- return list;
- }
-
- backTrack(s, 0, 0);
- return list;
- }
-
- public void backTrack(String s, int startIndex, int pointNum){
- if(pointNum == 3){ // 分割数量为3时,分隔结束
- if(isValid(s, startIndex, s.length() - 1)){ // 判断第四段⼦字符串是否合法
- list.add(s);
- }
- return;
- }
-
- for(int i = startIndex; i < s.length(); i++){
- if(!isValid(s, startIndex, i)){
- continue;
- }
- s = s.substring(0, i + 1) + "." + s.substring(i + 1); //在str的后⾯插⼊⼀个点
- pointNum++;
- backTrack(s, i + 2, pointNum); // 插⼊点之后下⼀个⼦串的起始位置为i+2
- pointNum--; // 回溯
- s = s.substring(0, i + 1) + s.substring(i + 2); // 回溯删掉逗点
- }
- }
-
- public Boolean isValid(String s, int left, int right) {
- if (left > right) {
- return false;
- }
- if (s.charAt(left) == '0' && left != right) { // 0开头的数字不合法
- return false;
- }
- int num = 0;
- for (int i = left; i <= right; i++) {
- num = num * 10 + (s.charAt(i) - '0');
- if (num > 255) { // 如果⼤于255了不合法
- return false;
- }
- }
- return true;
- }
- }
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例:输入:n = 4 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
- class Solution {
- List
> list = new ArrayList<>();
-
- public List
> solveNQueens(int n) {
- char[][] matrix = new char[n][n];
- for(char[] array : matrix){
- Arrays.fill(array, '.');
- }
- backTrack(n, 0, matrix);
- return list;
- }
-
- public void backTrack(int n, int row, char[][] matrix){
- if(row == n){
- List
item = new ArrayList<>(); - for(char[] array : matrix){
- item.add(String.valueOf(array));
- }
- list.add(item);
- return;
- }
-
- for(int column = 0; column < n; column++){
- if(isValid(row, column, n, matrix)){
- matrix[row][column] = 'Q';
- backTrack(n, row + 1, matrix);
- matrix[row][column] = '.';
- }
- }
- }
-
- public boolean isValid(int row, int column, int n, char[][] matrix) {
- // 检查列
- for(int i = 0; i < row; i++){
- if(matrix[i][column] == 'Q'){
- return false;
- }
- }
-
- // 检查45度对角线
- for(int i = row - 1, j = column - 1; i >= 0 && j >= 0; i--, j--) {
- if(matrix[i][j] == 'Q'){
- return false;
- }
- }
-
- // 检查135度对角线
- for(int i = row - 1, j = column + 1; i >= 0 && j <= n-1; i--, j++){
- if(matrix[i][j] == 'Q'){
- return false;
- }
- }
- return true;
- }
- }