LeetCode 78 子集

1、在使用回溯算法时,循环中,对于数组和字符串的处理是不一样的

数组是在回溯时index=i+1,而字符串是在循环时便加1,然后回溯使用的index=i。(子集使用的是i+1)

2、似乎使用new ArrayList<>(LinkedList)可以将链表转成一个新的线性表(这是因为ArrayList是直接指向地址,后续的操作也会影响到前面的结果)

同理在该题中,利用new ArrayList<>(HashSet)也可以将哈希set转成一个新的线性表(该题更多的是注重形式上的转化)

class Solution {
    Set<List<Integer>> result = new HashSet<>();
    LinkedList<Integer> temp = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        helper(nums, 0);
        return new ArrayList<>(result);

    }

    public void helper(int[] nums, int index) {
        result.add(new ArrayList<>(temp));
        if(index == nums.length ) {
             //result.add(new ArrayList<>(temp));
             return;
        }

        for(int i=index;i<nums.length;i++) {
            //result.add(new ArrayList<>(temp));
            temp.add(nums[i]);
            helper(nums, i+1);
            temp.removeLast();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值