题解 | #加起来和为目标值的组合(四)#

加起来和为目标值的组合(四)

https://www.nowcoder.com/practice/7a64b6a6cf2e4e88a0a73af0a967a82b

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @param target int整型 
     * @return int整型
     */
    // ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    int res = 0;
    LinkedList<Integer> path = new LinkedList<Integer>();
    public int combination (int[] nums, int target) {
        getCombine(target, nums);
        return res;
    }

    private void getCombine(int target, int[] nums) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            res++;
            return;
        }
        for (int i = 0; i < nums.length; ++i) {
            path.add(nums[i]);
            getCombine(target - nums[i], nums);
            path.removeLast();
        }
    }
}

全部评论

相关推荐

11-23 15:14
中原工学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务