LeetCode:39. Combination Sum(Medium)

1. 原题链接

https://leetcode.com/problems/combination-sum/description/

2. 题目要求

给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合

注意:(1)数组中的每一个元素可以重复用;(2)数组中不存在重复元素;(3)数组中都是正整数

3. 解题思路

采用迭代的方法检验所有元素组合

4. 代码实现

 1 import java.util.ArrayList;
 2 import java.util.List;
 3
 4 public class CombinationSum39 {
 5     public static void main(String[] args) {
 6         CombinationSum39 cs = new CombinationSum39();
 7         int[] candidates = {2,3,6,7};
 8         for (List l1:cs.combinationSum(candidates,7)){
 9             System.out.println(l1.toString());
10             System.out.println();
11         }
12
13     }
14     public List<List<Integer>> combinationSum(int[] candidates, int target) {
15         List<List<Integer>> result = new ArrayList<List<Integer>>();
16         combinationSum(result,new ArrayList<Integer>(),candidates,target,0);
17         return result;
18     }
19     public void combinationSum(List<List<Integer>> result, List<Integer> cur, int[] candidates, int target,int start) {
20         if (target > 0) {
21             for (int i = start;i < candidates.length;i++) {
22                 cur.add(candidates[i]);
23                 combinationSum(result, cur, candidates, target-candidates[i],i);
24                 cur.remove(cur.size() - 1);
25             }
26         }
27         if (target == 0)
28             result.add(new ArrayList<Integer>(cur));
29     }
30 }

原文地址:https://www.cnblogs.com/huiAlex/p/8227434.html

时间: 2024-08-04 23:29:31

LeetCode:39. Combination Sum(Medium)的相关文章

LeetCode: Longest Palindromic Substring(Medium)

原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!! 例如,"aabaa"是一个奇数个字符的回文字符串,他的中心只有一个字符"b". "aabbaa"是一个偶数个字符的回文字符串,他的中心却有两个相同字符"bb" 3. 思路

LeetCode:9. Palindromic Number(Medium)

原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) 2. 注意:负数不是回文!!因为前面有负号!注意整数溢出问题. 3. 思路:依然采用取余取整的方法 1 package com.huiAlex; 2 3 public class PalindromeNumber3 { 4 public static void main(String[] args

LeetCode:40. Combination Sum II(Medium)

1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合 注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次:(2)数组存在重复元素:(3)数组中都是正整数:(4)不能存在重复解 3. 解题思路 这与第39题 Combination Sum 看起来很是类似,但一些细节要求完全不

[Leetcode][Python]39: Combination Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers

LeetCode 39. Combination Sum (组合的和)

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (in

LeetCode OJ 39. Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

LeetCode:20. Valid Parentheses(Easy)

1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', '{', '}', '[' 和 ']'. 合法:形如"()[]"."{[()]}" 不合法:形如"([)]"."[[((" 判断所给字符串s是否合法. 3. 解题思路 对字符串s转换成字符数字进行遍历. 利用栈,遇到左半边字符:'

LeetCode:27. Remove Element(Easy)

1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val,删除数组中与val相同的元素,并返回删除后的数组长度 注意:不能定义新的数组,只能使用O(1)空间大小 3. 解题思路 遍历一次,将每个元素与给定的value进行比较,不同则给nums[count++]赋予当前元素的值:相同则直接跳过,最后返回count,即为删除后数组的长度. 4. 代码实现 p

LeetCode: 63. Unique Paths II(Medium)

1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/ 原文地址:https://www.cnblogs.com/huiAlex/p/8437069.html