combination-sum-ii(熟悉下Java排序)
代码还是这一块代码,但是还是写的很慢。。
其中用到了Java对 List的排序。查了很久,发现使用 Collections.sort 很方便。
另外对结果的去重,使用了 Java的HashSet.
https://leetcode.com/problems/combination-sum-ii/
package com.company; import java.util.*; class Solution {
Map<String, Set<List<Integer>>> mp;
int[] nums; Set<List<Integer>> impl(int start, int target) {
String key = "" + start + "_" + target;
if (mp.containsKey(key)) {
return mp.get(key);
} Set<List<Integer>> ret;
if (start < nums.length) {
ret = new HashSet<>();
ret.addAll(impl(start+1, target));
}
else {
ret = new HashSet<>();
} if (start > 0) {
if (target == nums[start - 1]) {
List<Integer> item = new ArrayList<>();
item.add(nums[start - 1]);
//print(item, 1);
ret.add(item);
}
else if (start < nums.length && target > nums[start - 1]) {
Set<List<Integer>> tmpRet = impl(start + 1, target - nums[start - 1]);
Iterator<List<Integer>> iter = tmpRet.iterator();
while (iter.hasNext()) {
List<Integer> item = new ArrayList<>(iter.next());
//print(item, 2);
item.add(nums[start - 1]);
Collections.sort(item); //print(item, 3);
//System.out.println("Start " + start + " target " + target);
ret.add(item);
}
}
} /*System.out.println("Begin ret:" + key);
Iterator<List<Integer>> iter = ret.iterator();
while (iter.hasNext()) {
print(iter.next(), 4);
}
System.out.println("End ret:" + key);*/
mp.put(key, ret);
return ret;
} public List<List<Integer>> combinationSum2(int[] candidates, int target) {
nums = candidates;
mp = new HashMap<>();
List<List<Integer>> ret = new ArrayList<>();
if (candidates.length == 0) {
return ret;
} impl(0, target);
String key = "" + 0 + "_" + target;
ret = new ArrayList<>(mp.get(key));
return ret;
} void print(List<Integer> item, int flag) {
System.out.printf("Here %d:", flag);
Iterator iter = item.iterator();
while (iter.hasNext()) {
System.out.printf("%d,", iter.next());
}
System.out.println();
}
} public class Main { public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution(); int[] cand = {10,1,2,7,6,1,5};
int target = 8;
List<List<Integer>> ret = solution.combinationSum2(cand, 8);
System.out.printf("Get ret: %s\n", ret.size()); Iterator<List<Integer>> iterator = ret.iterator();
while (iterator.hasNext()) {
Iterator iter = iterator.next().iterator();
while (iter.hasNext()) {
System.out.printf("%d,", iter.next());
}
System.out.println();
} System.out.println(); }
}
combination-sum-ii(熟悉下Java排序)的更多相关文章
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- 11.1Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 主网页的框架搭建,任务编号760 研究代码,学习相应语言,讨论设计思路 申开亮 学习数据库的操作,任务编号761 研究代码,学习相应语言,讨论设计思路 王 ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- HTTP - 内容编码
HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...
- as3.0服务端FMS软件常用的方法与属性参考示例
转自:http://www.cuplayer.com/player/PlayerCode/RTMP/2012/0918429.html Application类的方法汇总方法 描述Applicatio ...
- ubuntu 12.04 搭建nginx + php + mysql +phpmyadmin
1. 使用官方PPA安装 Nginx 最新版本,使用以下命令:sudo add-apt-repository ppa:nginx/stablesudo apt-get updatesudo apt-g ...
- 高性能网络编程1----accept建立连接
转 http://taohui.org.cn/tcpperf1.html 陶辉 taohui.org.cn 回到应用层,往往只需要调用类似于accept的API就可以建立TCP连接.建立连接的流程大 ...
- LINUX下如何查看tomcat运行状态,判断其是否启动
1,查看Tomcat启动日志. ${catalina_home}\logs [root@iZ25b4ffkfaZ logs]# tail -f catalina.outSep 10, 2015 11: ...
- Dear Project Manager, I Hate You
项目经理,我恨你,而且我知道你也恨我.我真的不理解,你究竟是做什么的. 你是一个多么独特的角色呀,几乎每个公司都要雇用你这样的人.可在不管大大小小的项目中,你与其说是帮忙,不如说是添乱.我坚信,大部分 ...
- Highcharts中初始化最大值与最小值的柱状图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- codeforces D
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...