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 ...
随机推荐
- 分布式文件系统 - FastDFS
分布式文件系统 - FastDFS 别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白 ...
- C#如何判断两个数组相等
/// <summary> /// 数组比较是否相等 /// </summary> /// <param name="bt1">数组1</ ...
- 玩转SmartQQ之登录
SmartQQ是腾讯新出的一个WebQQ,登录地址是:http://w.qq.com/,目前之前的WebQQ可以继续使用,登录地址:http://web2.qq.com/webqq.html,Smar ...
- 【转】android如何浏览并选择图片 音频 视频
转自:http://www.cnblogs.com/top5/archive/2012/03/06/2381986.html 这几天 在学习并开发android系统的图片浏览 音频 视频 的浏览 ...
- 视频学习_css基础学习
块状元素 block element 容器元素 设置高宽 width height 可以容纳 文本 内脸 和其他块状 霸道 独占一行 特例:form 只容纳 块状元素 常见元素 http:// ...
- 3243: [Noi2013]向量内积 - BZOJ
Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压
题目链接: 题目 E. Another Sith Tournament time limit per test2.5 seconds memory limit per test256 megabyte ...
- 在线最优化求解(Online Optimization)之三:FOBOS
在线最优化求解(Online Optimization)之三:FOBOS FOBOS (Forward-Backward Splitting)是由John Duchi和Yoram Singer提出的[ ...
- Maven开源中国镜像
mirrors> <mirror> <id>nexus-osc</id> <mirrorOf>central</mirrorOf> ...
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量 ...