训练营第二十七天回溯(排列)

训练营第二十七天回溯(排列)

491.递增子序列

力扣题目链接(opens new window)

题目

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。

示例:

  • 输入: [4, 6, 7, 7]
  • 输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

说明:

  • 给定数组的长度不会超过15。
  • 数组中的整数范围是 [-100,100]。
  • 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。

解答

方法一:
  • 使用hashSet记录横向,来确保同一层不会选择相同元素

    HashSet<Integer> hs = new HashSet<>();
    			for (int i = startIndex; i < nums.length; i++) {
    				if (i > startIndex && hs.contains(nums[i])) {//对横向的判断,使用hashSet保留该轮之前的元素
    					continue;
    				}
    				hs.add(nums[i]);
    
  • 深度不同的还是与之前相同

    if (path.size() >= 2) {
    				if (path.getLast() < path.get(path.size() - 2))//对深度的结束的判断
    					return;
    				results.add(new ArrayList<>(path));
    			}
    
	class Solution {
		List<List<Integer>> results = new ArrayList<>();
		LinkedList<Integer> path = new LinkedList<>();

		public List<List<Integer>> findSubsequences(int[] nums) {
			backtracking(nums, 0);
			return results;
		}

		void backtracking(int[] nums, int startIndex) {
			if (path.size() >= 2) {
				if (path.getLast() < path.get(path.size() - 2))//对深度的结束的判断
					return;
				results.add(new ArrayList<>(path));
			}
			HashSet<Integer> hs = new HashSet<>();
			for (int i = startIndex; i < nums.length; i++) {
				if (i > startIndex && hs.contains(nums[i])) {//对横向的判断,使用hashSet保留该轮之前的元素
					continue;
				}
				hs.add(nums[i]);
				path.add(nums[i]);
				backtracking(nums, i + 1);
				path.removeLast();
			}
		}
	}
方法二:
  • 使用数组记录横向,来确保同一层不会选择相同元素,与hashset类似(前提是要求中说了nums[i]的范围是从-100到100)

    int[] set = new int[201];
    			for (int i = startIndex; i < nums.length; i++) {
    				if (i > startIndex && set[nums[i] + 100] == 1) {//对横向的判断,使用hashSet保留该轮之前的元素
    					continue;
    				}
    				set[nums[i] + 100] = 1;
    
  • 深度不同的还是与之前相同

	class Solution {
		List<List<Integer>> results = new ArrayList<>();
		LinkedList<Integer> path = new LinkedList<>();

		public List<List<Integer>> findSubsequences(int[] nums) {
			backtracking(nums, 0);
			return results;
		}

		void backtracking(int[] nums, int startIndex) {
			if (path.size() >= 2) {
				if (path.getLast() < path.get(path.size() - 2))//对深度的结束的判断
					return;
				results.add(new ArrayList<>(path));
			}
			int[] set = new int[201];
			for (int i = startIndex; i < nums.length; i++) {
				if (i > startIndex && set[nums[i] + 100] == 1) {//对横向的判断,使用hashSet保留该轮之前的元素
					continue;
				}
				set[nums[i] + 100] = 1;
				path.add(nums[i]);
				backtracking(nums, i + 1);
				path.removeLast();
			}
		}
	}

46.全排列

力扣题目链接(opens new window)

题目

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例 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 中的所有整数 互不相同

解答

判断递归中的元素是否重复的方法

  • 使用hash映射判断元素
  • 直接对path中的元素进行判断
  • 直接记录数组下标,判断该下标是否被使用
方法一
//使用数组代替hashset的前提是已知nums中的元素范围 `-10 <= nums[i] <= 10` (使用数组会更快)
class Solution {
	List<List<Integer>> results = new ArrayList<>();
	LinkedList<Integer> path = new LinkedList<>();
	int[] set = new int[21];
    public List<List<Integer>> permute(int[] nums) {
		backtracking(nums);
		return results;
    }

	void backtracking(int[] nums){
		if (path.size() == nums.length){
			results.add(new ArrayList<>(path));
			return;
		}
		for (int i = 0; i < nums.length; i++) {
			if (set[nums[i] +10] == 1)
				continue;
			set[nums[i] +10] = 1;
			path.add(nums[i]);
			backtracking(nums);
			path.removeLast();
			set[nums[i] +10] = 0;
		}
	}
}


class Solution {
	List<List<Integer>> results = new ArrayList<>();
	LinkedList<Integer> path = new LinkedList<>();
	HashSet<Integer> set = new HashSet<>();

    public List<List<Integer>> permute(int[] nums) {
		backtracking(nums);
		return results;
    }

	void backtracking(int[] nums){
		if (path.size() == nums.length){
			results.add(new ArrayList<>(path));
			return;
		}
		for (int i = 0; i < nums.length; i++) {
			if (set.contains(nums[i]))
				continue;
			set.add(nums[i]);
			path.add(nums[i]);
			backtracking(nums);
			path.removeLast();
			set.remove(nums[i]);

		}
	}
}
方法二
class Solution {
	List<List<Integer>> results = new ArrayList<>();
	LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
		backtracking(nums);
		return results;
    }

	void backtracking(int[] nums){
		if (path.size() == nums.length){
			results.add(new ArrayList<>(path));
			return;
		}
		for (int i = 0; i < nums.length; i++) {
			if (path.contains(nums[i])) continue;
			path.add(nums[i]);
			backtracking(nums);
			path.removeLast();
		}
	}
}
方法三
class Solution {
	List<List<Integer>> results = new ArrayList<>();
	LinkedList<Integer> path = new LinkedList<>();
	boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
		used = new boolean[nums.length];
		backtracking(nums);
		return results;
    }

	void backtracking(int[] nums){
		if (path.size() == nums.length){
			results.add(new ArrayList<>(path));
			return;
		}
		for (int i = 0; i < nums.length; i++) {
			if (used[i]) continue;
			used[i] = true;
			path.add(nums[i]);
			backtracking(nums);
			path.removeLast();
			used[i] = false;
		}
	}
}

47.全排列 II

力扣题目链接(opens new window)

题目

给定一个可包含重复数字的序列 nums ,按任意顺序返回所有不重复的全排列。

示例 1:

  • 输入:nums = [1,1,2]
  • 输出: [[1,1,2], [1,2,1], [2,1,1]]

示例 2:

  • 输入:nums = [1,2,3]
  • 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

解答

都是使用类似于hash映射的方式判断

  • 对于树枝的元素选取,要在backtracking函数之前定义数组
  • 对于深度相同元素的选取,要在backtracking中定义,因为只要进入下一层就会重新定义一个新数组,并且不需要回溯
class Solution {
	List<List<Integer>> results = new ArrayList<>();
	LinkedList<Integer> path = new LinkedList<>();
	boolean[] used;//用于判断树深相同元素
    public List<List<Integer>> permuteUnique(int[] nums) {
		used = new boolean[nums.length];
		backtracking(nums);
		return results;
    }
	void backtracking(int[] nums){
		if (path.size() == nums.length){
			results.add(new ArrayList<>(path));
			return;
		}
		boolean[] set = new boolean[21];//用于判断同一层重复元素(如果不知道num中的元素范围要使用hashset)
		for (int i = 0; i < nums.length; i++) {
			if (used[i]) continue;
			if (set[nums[i] + 10]) continue;
			set[nums[i] + 10] = true;
			used[i] = true;
			path.add(nums[i]);
			backtracking(nums);
			used[i] = false;
			path.removeLast();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值