leetcode回溯

回溯算法学习

描述:在算法问题中,先试探某一条路径是否可行,不可行则回到上一步换一条路径再次尝试。

在回溯算法中,不断向前尝试,失败则回退继续尝试,一直到所有的可能性尝试完后得到最终的结果。

算法示例:八皇后问题。

leetcode中两个相似的回溯算法问题:

  1. 给定集合的所有子集

  2. 给定整数(数组)的指定长度的所有组合。给定的整数相当于从 1n 的数组。

    例如:给定整数 4,指定组合长度 2 , 则所有组合为[[1, 2], [1, 3], [1, 4], [2, ,3], [2, 4], [3, 4]];

​ 在leetcode中,上述两个问题的采用回溯法,利用递归基本代码十分相似。

采用回溯法中,现在集合中加入当前元素,在尝试下一个元素,回溯到当前元素时,集合中没有当前元素,然后再次向下尝试。(这两个问题的相似点都是在一条路径的某个节点,都是产生有或没有当前元素两种情况)。

LeetCode: Combination

public class Combination {
    private List<List<Integer>> result;

    public List<List<Integer>> combine(int n, int k) {
        result = new ArrayList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        combineRecursive(n, k, list, 1);
        return result;
    }

    public void combineRecursive(int n, int k, List<Integer> list, int curPos) {
        if (list.size() == k) {
            result.add(new ArrayList<Integer>(list));
            return;
        }
        if (curPos <= n) {
        //有当前元素的组合
        list.add(curPos);
        combineRecursive(n, k, list, curPos + 1);
        //没有当前元素的组合
        list.remove(new Integer(curPos));
        combineRecursive(n, k, list, curPos + 1);
        }
    }
}

LeetCode: Subsets

public class Subset {
    private List<List<Integer>> result;

    public List<List<Integer>> subset(int[] nums) {
        result = new ArrayList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        subsetRecursive(nums, 0, list);
        return result;
    }

    public void subsetRecursive(int[] nums, int pos,   List<Integer> list) {
        if (pos == nums.length) {
            result.add(new ArrayList<Integer>(list));
            return;
        }
        //当前位置有该元素
        list.add(nums[pos]);
        subsetRecursive(nums, pos + 1, list);
        //当前位置没有该元素
        list.remove(new Integer(nums[pos]));
        subsetRecursive(nums, pos + 1, list);
    }
}

其他类似问题有待添加!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值