Uber面试题:数字组合(还算简单)

给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合。

在同一个组合中, candidates 中的某个数字不限次数地出现。

  • 所有数值 (包括 target ) 都是正整数.
  • 返回的每一个组合内的数字必须是非降序的.
  • 返回的所有组合之间可以是任意顺序.
  • 解集不能包含重复的组合.

点此在线做题

样例 1:

输入: candidates = [2, 3, 6, 7], target = 7
输出: [[7], [2, 2, 3]]

样例 2:

输入: candidates = [1], target = 3
输出: [[1, 1, 1]]

算法:DFS

解题思路

  • 本题需要在给定一个候选数字的集合 candidates 中. 找到所有的和为目标值 target 的组合。
    对于这种需要求解所有结果的问题,我们为了避免结果有遗漏或者重复,一般可以考虑使用搜索算法。而本题需要- 满足组合中选取的元素之和为一个目标值,这个可以作为深度优先搜索的边界判断条件,故本题可以使用深度优先搜索(DFS)算法。

算法流程

1.由于题目候选数字的集合candidates可能包含重复数字,且返回的结果要求组合内数字非降序,因此首先需对candidates进行升序排序并去重,得到新的数字集合candidatesNew;
2.对新的数字集合进行深度优先搜索,传入的参数包括:数字集合candidatesNew、当前的位置index、当前存入的组合current、距离目标值的差remainTarget、保存答案的列表result;

  • 当remainTarget=0即达到边界,将current添加到result,回溯;
  • 循环遍历index位置到数字集合的末尾,分别递归调用dfs;
  • 递归步进为:remainTarget - candidatesNew[i];
  • 剪枝:当发现当前的数字加入已超过remainTarget可进行剪枝。

复杂度分析

时间复杂度:O(n^target/min), (拷贝过程视作O(1)),n为集合中数字个数,min为集合中最小的数字
每个位置可以取集合中的任意数字,最多有target/min个数字。
空间复杂度:O(n^target /min),n为集合中数字个数,min为集合中最 小的数字
对于用来保存答案的列表,最多有n^target/min种组合

public class Solution {
    /**
     * @param candidates: A list of integers
     * @param target:An   integer
     * @return: A list of lists of integers
     */
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> results = new ArrayList<>();
        // 集合为空
        if (candidates.length == 0) {
            return results;
        }
        // 排序和去重
        int[] candidatesNew = removeDuplicates(candidates);
        // dfs
        dfs(candidatesNew, 0, new ArrayList<Integer>(), target, results);
        return results;
    }

    private int[] removeDuplicates(int[] candidates) {
        //排序
        Arrays.sort(candidates);
        //去重
        int index = 0;
        for (int i = 0; i < candidates.length; i++) {
            if (candidates[i] != candidates[index]) {
                candidates[++index] = candidates[i];
            }
        }
        int[] candidatesNew = new int[index + 1];
        for (int i = 0; i < index + 1; i++) {
            candidatesNew[i] = candidates[i];
        }
        return candidatesNew;
    }

    private void dfs(int[] candidatesNew, int index, List<Integer> current, int remainTarget, List<List<Integer>> results) {
        // 到达边界
        if (remainTarget == 0) {
            results.add(new ArrayList<Integer>(current));
            return;
        }
        // 递归的拆解:挑一个数放入current
        for (int i = index; i < candidatesNew.length; i++) {
            // 剪枝
            if (remainTarget < candidatesNew[i]) {
                break;
            }
            current.add(candidatesNew[i]);
            dfs(candidatesNew, i, current, remainTarget - candidatesNew[i], results);
            current.remove(current.size() - 1);
        }
    }
}

查看更多题解

相关推荐

巨硬家面试题:字符串查找(带解题思路)

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。 在线做题地址 说明 在面试中我是否需要实现KMP算法? 不需要,当这种问题出现在面试中时,面试官很可能只是想要测试一下你的基础应用能力。当然你需要先跟面试官确认清楚要怎么实现这个题。 《九章算法班》中讲过比KMP更简单的算法:Rabin-karp 算法,可免费试听。 样例 1: 输入: source = "source" ,

LintCode的博客 1230

阿里2020最新动态规划题:背包问题

在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]。 你不可以将物品进行切割。 点此在线做题 样例 1: 输入: [3,4,8,5], backpack size=10 输出: 9 样例 2: 输入: [2,3,5,7], backpack size=12 输出: 12 算法:DP 从已知的题目中,可以总结出以下两点: 每件物品只有一种 每件物品最多选择一次 那么考虑对于前i件的物品在容量为w的背包下,最大的装载量是多少,由此可以总结出对应的子结构,进

LintCode的博客 681

百度最新面试题:祖玛游戏思路详解

这是一个祖玛游戏。 一行球放在桌面上,球被涂上了红(R)、黄(Y)、蓝(B)、绿(G)和白(W)这么几种颜色,同时你也拥有几个球。 每一次,从你拥有的球当中拿出一个,插入到当前行当中(包括最左边和最右边)。然后,如果有三个或更多的同色球挨在一起,然后就消除这几个球。一直这么做直到没有更多的球可以消除。 找到最少的需要插入的球的数量,使得所有的球都可以被删除。如果不能删除所有的球,那么就返回-1。 初始的行不会拥有三个及以上的连续的同色球。 行中的球的数量不会超过20,用名为"borad"的输入字符串表示。

LintCode的博客 1080

LeetCode/LintCode 题解丨一周爆刷分治法:合并两棵二叉树

LeetCode/LintCode 题解丨一周爆刷分治法:合并两棵二叉树

LintCode的博客 328

LeetCode/LintCode 题解丨一周爆刷字符串:乱序字符串

LeetCode/LintCode 题解丨一周爆刷字符串:乱序字符串

LintCode的博客 330

LeetCode/LintCode 题解丨一周爆刷字符串:文字并排

LeetCode/LintCode 题解丨一周爆刷字符串:文字并排

LintCode的博客 326

LeetCode/LintCode 题解丨一周爆刷字符串:简化路径

LeetCode/LintCode 题解丨一周爆刷字符串:简化路径

LintCode的博客 1479

LeetCode/LintCode 题解丨一周爆刷字符串:神奇字符串

LeetCode/LintCode 题解丨一周爆刷字符串:神奇字符串

LintCode的博客 398

LeetCode/LintCode 题解丨一周爆刷字符串:查找和替换模式

LeetCode/LintCode 题解丨一周爆刷字符串:查找和替换模式

LintCode的博客 364

LeetCode/LintCode 题解丨一周爆刷字符串:旋转字符数组

LeetCode/LintCode 题解丨一周爆刷字符串:旋转字符数组

LintCode的博客 1676

LeetCode/LintCode 题解丨一周爆刷字符串:URL 编码

LeetCode/LintCode 题解丨一周爆刷字符串:URL 编码

LintCode的博客 760

LeetCode/LintCode 题解丨一周爆刷字符串:空格替换

LeetCode/LintCode 题解丨一周爆刷字符串:空格替换

LintCode的博客 294

LeetCode/LintCode 题解丨一周爆刷字符串:独特的摩尔斯编码

LeetCode/LintCode 题解丨一周爆刷字符串:独特的摩尔斯编码

LintCode的博客 293

LeetCode/LintCode 题解丨一周爆刷双指针:寻找重复的数

LeetCode/LintCode 题解丨一周爆刷双指针:寻找重复的数

LintCode的博客 375

LeetCode/LintCode 题解丨一周爆刷双指针:神奇字符串

LeetCode/LintCode 题解丨一周爆刷双指针:神奇字符串

LintCode的博客 333

LeetCode/LintCode 题解丨一周爆刷双指针:最小范围

LeetCode/LintCode 题解丨一周爆刷双指针:最小范围

LintCode的博客 1909

LeetCode/LintCode 题解丨一周爆刷双指针: 两数之和

LeetCode/LintCode 题解丨一周爆刷双指针: 两数之和

LintCode的博客 274

LeetCode/LintCode 题解丨一周爆刷双指针:K个空的位置

LeetCode/LintCode 题解丨一周爆刷双指针:K个空的位置

LintCode的博客 308

LeetCode/LintCode 题解丨一周爆刷双指针:数组中的最长山脉

LeetCode/LintCode 题解丨一周爆刷双指针:数组中的最长山脉

LintCode的博客 299

2022年Apple春招真题:二叉树的层次遍历

2022年Apple春招真题:二叉树的层次遍历

LintCode的博客 321

2022年美团春招真题:字符串排序

2022年美团春招真题:字符串排序

LintCode的博客 289

2022年百度春招真题:最长上升子序列

2022年百度春招真题:最长上升子序列

LintCode的博客 221

2022年微软面试真题:最大子数组

描述: 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。 每个子数组的数字在数组中的位置应该是连续的。 样例 1: 输入: nums = [−2,2,−3,4,−1,2,1,−5,3] 输出: 6 解释: 符合要求的子数组为[4,−1,2,1],其最大和为 6。 样例 2: 输入: nums = [1,2,3,4] 输出: 10 解释: 符合要求的子数组为[1,2,3,4],其最大和为 10。 挑战 要求时间复杂度为O(n) ...

LintCode的博客 381

亚马逊面试真题:二叉树的层次遍历

描述: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 首个数据为根节点,后面接着是其左儿子和右儿子节点值, "#"表示不存在该子节点。 节点数量不超过20。 在线测评地址 样例 1: 输入: tree = {1,2,3} 输出: [[1],[2,3]] 解释: 1 / \ 2 3 它将被序列化为{1,2,3} 样例 2: 输入: tree = {1,#,2,3} 输出: [[1],[2],[3]] 解释: 1 \ 2 / 3 它将被序列化为{1,#,2,3} 挑

LintCode的博客 409

亚马逊经典真题:三数之和

描述: 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。 在线测评地址 样例 1: 输入: numbers = [2,7,11,15] 输出: [] 解释: 找不到三元组使得三个数和为0。 样例 2: 输入: numbers = [-1,0,1,2,-1,-4] 输出: [[-1, 0, 1],[-1, -1, 2]] 解释: [-1, 0, 1]和[-1, -1, 2]是符合条件的2个三元组。 源代码: class Solution:

LintCode的博客 520

谷歌真题回顾:下一个排列

不能错过的谷歌面试真题

LintCode的博客 289

微软面试真题:全排列

解决面试中的全排列问题

LintCode的博客 220

亚马逊面试宝典:两数之和

描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。 在线测评地址 样例 1: 输入: numbers = [2,7,11,15] target = 9 输出: [0,1] 解释: numbers[0] + numbers[1] = 9 样例 2: 输入: numbers = [15,2,7,11] target = 9 输出: [1,2] 解释: num

LintCode的博客 160

谷歌面试真题分享

谷歌面试题:插入区间 描述: 给出一个无重叠的按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。 在线测评地址 样例 1: 输入: 区间列表 = [(1,2), (5,9)] 新的区间 = (2, 5) 输出: [(1,9)] 解释: 插入后区间有重叠,需要合并区间。 样例 2: 输入: 区间列表 = [(1,2), (5,9)] 新的区间 = (3, 4) 输出: [(1,2), (3,4), (5,9)] 解释: 区间按

LintCode的博客 481

微软面试真题分享

想进入大厂工作,你不能错过的经验分享

LintCode的博客 259
上一篇: 微软面试题:两个排序数组的中位数
下一篇: Google面试题:二叉搜索树中最接近的值
LintCode
LintCode 企业官方账号 企业官方账号
博客等级 码龄6年 15粉丝 113原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址