[Swift]LeetCode39. 组合总和 | Combination Sum

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9900712.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[[7],[2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[[2,2,2,2],[2,3,3],[3,5]
]

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

  • 所有数字(包括 target)都是正整数。
  • 解集不能包含重复的组合。 

示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[[7],[2,2,3]
]

示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[[2,2,2,2],[2,3,3],[3,5]
]

20ms
 1 class Solution {
 2     var result = [[Int]]()
 3     func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
 4         combinationSum(candidates, target, 0, [Int]())
 5         return result
 6     }
 7     
 8     func combinationSum(_ candidates: [Int], _ target: Int, _ currentInex: Int, _ usdedNums: [Int]) {
 9         if target <= 0  {
10             if target == 0 {
11                 result.append(usdedNums)               
12             }
13             return
14         }
15         for i in currentInex..<candidates.count {
16             let currentValue = candidates[i]
17             if currentValue > target {
18                 continue
19             }
20             var usdedNumsCopy = usdedNums
21             usdedNumsCopy.append(currentValue)
22             combinationSum(candidates, target-currentValue, i, usdedNumsCopy)
23         }
24     }
25 }

24ms

 1 class Solution {
 2     func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
 3         let candidatesCopy = candidates.sorted{ $0 < $1 }
 4         var tmp = [Int]()
 5         var res = [[Int]]()
 6         var index = 0
 7         helper(&tmp, &res, index, candidatesCopy, target)
 8         return res
 9     }
10     
11     private func helper(_ tmp: inout [Int], _ res: inout [[Int]], _ index: Int, _ candidatesCopy: [Int], _ target: Int) {
12         if target == 0 {
13             res.append(tmp)
14             return
15         }else if index == candidatesCopy.count {
16             return
17         }
18         
19         for i in index..<candidatesCopy.count {
20             if candidatesCopy[i] > target {
21                 return
22             }else if i != index && candidatesCopy[i] == candidatesCopy[i - 1] {
23                 continue
24             }
25             
26             tmp.append(candidatesCopy[i])
27             helper(&tmp, &res, i, candidatesCopy, target - candidatesCopy[i])
28             tmp.removeLast()
29         }
30     }
31 }

28ms

 1 class Solution {
 2     func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
 3         var result = [[Int]]()
 4         var out = [Int]()
 5         var candidates = candidates.sorted()
 6         combinationSumDFS(candidates, target, 0, &out, &result)
 7         return result
 8     }
 9     
10     func combinationSumDFS(_ candidates: [Int], _ target: Int, _ start: Int, _ out: inout [Int], _ res: inout [[Int]]) {
11         if target == 0 {
12             res.append(out)
13         } else {
14             for i in start..<candidates.count {
15                 guard target - candidates[i] >= 0 else {
16                     break
17                 }
18                 out.append(candidates[i])
19                 combinationSumDFS(candidates, target - candidates[i], i, &out, &res)
20                 out.remove(at: out.count - 1)
21                 
22             }
23         }
24     }
25     
26 }

28ms

 1 class Solution {
 2     
 3     var list = [[Int]]()
 4     
 5     func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
 6         var currentlySelected = [Int]()
 7         recursiveCombSum(candidates: candidates, index: 0, target: target, currentlySelected: currentlySelected)
 8         return list
 9     }
10     
11     func recursiveCombSum(candidates: [Int], index: Int, target: Int, currentlySelected: [Int]) {
12         if 0 == target {
13             list += [currentlySelected]
14         }
15         if index == candidates.count {
16         } else {
17             for i in index..<candidates.count {
18                 if candidates[i] <= target {
19                     var newTarget = target - candidates[i]
20                     var newList = currentlySelected + [candidates[i]]
21                     recursiveCombSum(candidates: candidates, index: i, target: newTarget, currentlySelected: newList)
22                 }
23             }
24         }
25     }
26 }

56ms

 1 class Solution {
 2     var conbineArray = [[Int]]()
 3     func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
 4         let count = candidates.count
 5         guard count > 0 else {
 6             return [[Int]]()
 7         }
 8         combine(candidates, [Int](), count, 0, target)
 9         return conbineArray
10     }
11     
12     func combine(_ candidates: [Int], _ currentCombine: [Int], _ count: Int, _ index: Int, _ target: Int) {
13         if target < 0 { return }
14         if index == count { return }
15         if target == 0 {
16             conbineArray.append(currentCombine)
17             return
18         }
19         
20         combine(candidates, currentCombine, count, index + 1, target)
21         var currentCombine = currentCombine
22         currentCombine.append(candidates[index])
23         combine(candidates, currentCombine, count, index, target - candidates[index])
24     }
25 }

 

转载于:https://www.cnblogs.com/strengthen/p/9900712.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352315.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

eclipse 重构_Eclipse对类固醇的重构

eclipse 重构在上一篇有关常见Java违规的文章中 &#xff0c;我列出了Java开发人员容易犯的一系列错误。 在重构Java项目以解决这些违规问题的同时&#xff0c;我广泛使用Eclipse的重构功能来快速更改代码。 下面是这种重构技术的汇编。 1.在块级语句周围添加花括号 用{curly …

python中能够处理的最大整数是_实例讲解Python中整数的最大值输出

在Python中可以存储很大的值&#xff0c;如下面的Python示例程序&#xff1a;x 10000000000000000000000000000000000000000000;x x 1print (x)输出&#xff1a;10000000000000000000000000000000000000000001在Python中&#xff0c;整数的值不受位数的限制&#xff0c;可以…

SAS宏保存以便快速调用的三种解决方案(转载)

1.方式一&#xff1a;%include %include "full_path\sortds.txt"; inserts any code in the file called sortds.txt into your program at the location of the %include statement. Using this method, the macro must be recompiled every time a %INCLUDE is exe…

log4j.properties log4j.xml 路径问题

自动加载配置文件&#xff1a; &#xff08;1&#xff09;如果采用log4j输出日志&#xff0c;要对log4j加载配置文件的过程有所了解。log4j启动时&#xff0c;默认会寻找source folder下的log4j.xml配置文件&#xff0c;若没有&#xff0c;会寻找log4j.properties文件。然后加…

webpack4+react多页面架构

webpack在单页面打包上应用广泛&#xff0c;以create-react-app为首的脚手架众多&#xff0c;单页面打包通常是将业务js&#xff0c;css打包到同一个html文件中&#xff0c;整个项目只有一个html文件入口,但也有许多业务需要多个页面不同的入口&#xff0c;比如不同的h5活动&am…

Spring安全性和密码编码

在以前的文章中&#xff0c;我们深入探讨了Spring安全性。 我们实现了由jdbc支持的安全性&#xff0c;基于自定义 jdbc查询的安全性以及从nosql数据库检索安全性的信息。 通过足够小心&#xff0c;我们会发现密码为纯文本格式。 尽管这在实际环境中可以很好地用于示例目的&…

SAS宏技术中,%let和call symput有什么区别?

平时经常使用的宏变量定义方法有三种&#xff1a; 1. %let xxxyyy; 2. Call Symput(xxx,yyy); 3. select xxx into: yyy. 三种定义方式最大的区别是在MACRO函数内定义所生成的宏变量的类型不同&#xff1a; Call Symput在宏函数中定义的宏变量可以在函数外调用&#xff1b;而%…

阿里巴巴的开源项目Druid(关于数据库连接)

原文地址&#xff1a;http://www.iteye.com/magazines/90文章简介 Druid首先是一个数据库连接池&#xff0c;但它不仅仅是一个数据库连接池&#xff0c;它还包含一个ProxyDriver&#xff0c;一系列内置的JDBC组件库&#xff0c;一个SQLParser。Druid支持所有JDBC兼容的数据库&a…

springcloud服务注册和发现

微服务架构中&#xff0c;服务发现组件是一个非常关键的组件&#xff0c;服务消费者、服务提供者、服务发现组件的关系大致如下&#xff1a; 各个微服务启动时&#xff0c;将自己的网络地址等信息注册到服务发现组件中&#xff0c;服务发现组件会存储这些信息服务消费者可从服务…

sas infile和filename

3.1 追加原始文件 原始数据可以使用以下的方法进行纵合并。INFILE语句 FILENAME语句 FILEVAR选项 操作系统自身的技术 首先&#xff0c;你可能要察看原始数据。可以用FSLIST过程。 语法&#xff1a; PROC FSLIST FILE file-specification; RUN; 实际使用中&#xff0c;专门的编…

Java 多线程(六) synchronized关键字详解

多线程的同步机制对资源进行加锁&#xff0c;使得在同一个时间&#xff0c;只有一个线程可以进行操作&#xff0c;同步用以解决多个线程同时访问时可能出现的问题。 同步机制可以使用synchronized关键字实现。 当synchronized关键字修饰一个方法的时候&#xff0c;该方法叫做同…

java自动化_作为测试工程师进阶自动化选Java还是Python?

这是很多测试工程师从功能跨入自动化纠结的问题&#xff0c;今天本文带大家一探究竟。Java和Python一直都是两种很火的语言&#xff0c;用Python的一定觉得Python好&#xff0c;用Java的只觉得Java好。Java语言 VS Python语言Java自动化方法 VS Python自动化方法综上所述&…

Spring –添加Spring MVC –第2部分

在上一部分中&#xff0c;我们为经理和员工实现了控制器。 既然我们知道了自己的出路&#xff0c;我们将做很少&#xff08;但仅做很少&#xff09;更复杂的事情-任务和时间表的控制器。 因此&#xff0c;让我们从org.timesheet.web开始。 TaskController 。 首先创建一个类&am…

CALL SYMPUT与CALL SYMPUTX区别

call symput 在data步中将值塞入宏变量http://www2.sas.com/proceedings/sugi29/052-29.pdf [SAS] CALL SYMPUT与CALL SYMPUTX CALL SYMPUT的功能是可以在DATA step内将值塞到一个macro变量里面。如果这个macro变量已经存在&#xff0c;那这个call就会更新该macro变量的值。 CA…

Linux基本结构

一个完整地Linux操作系统由4部分组成&#xff0c;即内核&#xff08;Kernel&#xff09;、外壳&#xff08;Shell&#xff09;、实用程序&#xff08;Utilities&#xff09;和应用程序&#xff08;Applications&#xff09;。 &#xff08;1&#xff09;内核是Linux的心脏&…

jmeter使用_jmeter工具的使用

1.本地下载到官网&#xff0c;5.3版本的对应的是jdk8版本&#xff0c;就用这个了2.解压进入bin目录&#xff0c;找到jmeter.bat启动它&#xff0c;会弹出两个窗口&#xff0c;一个是启动窗口&#xff0c;使用jmeter不可以关了它&#xff0c;另一个是jmeter的工作界面进入界面默…

UliPad 初体验----python 开发利器

学习python 有段时间&#xff0c;最近博客更新比较慢了&#xff0c;空闲时间在零零碎碎的学python &#xff0c;难成文&#xff0c;也就没整理成博客。 学习python 最苦恼的就是没有趁手IDE &#xff0c;之前学java 时 Eclipse 肯定是不二之选。eclipse pydev 也可以开发python…

Neo4j:找到两个纬度/经度之间的中点

在过去的两个周末中&#xff0c;我一直在处理一些运输数据&#xff0c;并且我想运行A *算法来查找两个车站之间的最快路线。 A *算法将一个EstimateEvaluator作为其参数之一&#xff0c;然后该评估器查看节点的经度/纬度&#xff0c;以确定一条路径是否值得遵循。 因此&#x…

猫狗大战

可行性背包 令dp[i][j]表示选i个人能否达到j这个状态&#xff0c;那么转移就和背包一样了&#xff0c;外层枚举选哪一个(K),2、3层枚举i&#xff0c;j&#xff0c;那么\[dp[i][j] | dp[i-1][j-val[k]];\] 转载于:https://www.cnblogs.com/bullshit/p/9902003.html

SAS的数组array介绍

SAS可以把一组同为数值型或同为字符型的变量合在一起&#xff0c;使用同一个名字称呼&#xff0c;用下标来区分。这与通常的程序设计语言中的数组略有区别&#xff0c;通常的程序设计语言中数组元素没有对应的变量名&#xff0c;而SAS数组每个元素都有自己的变量名。 一、数值型…