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 ...
随机推荐
- JAVA SSH 框架介绍
SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...
- C++拷贝构造函数详解(转载)
一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员 ...
- IntelliJ IDEA创建项目技巧(转)
转自:http://www.myext.cn/webkf/a_2539.html IntelliJ IDEA创建项目技巧 来源:网络 编辑:admin intellij idea教程 首先我要说 ...
- 【递推】BZOJ 1088: [SCOI2005]扫雷Mine
1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2275 Solved: 1328[Submit][St ...
- Matlab与科学计算的基本运算
各种允许的比较关系 >, >=, <, <=, ==,~=, find(), all(), any() 例:>> A=[1,2,3;4,5,6;7,8,0]A = ...
- oracle——分析函数——排序值分析函数
一.问题描述 查询列表时,我们有时需要对查询结果依据某个字段进行排名. 如果每条记录在排序字段上都不相同,我们可以将原查询作为一个视图,查询其rownum,便可以实现简单排序,例如: select r ...
- [设计模式] 7 适配器模式 adapter
在 Adapter 模式的结构图中可以看到,类模式的 Adapter 采用继承的方式复用 Adaptee的接口,而在对象模式的 Adapter 中我们则采用组合的方式实现 Adaptee 的复用 类模 ...
- HDU 2136 Largest prime factor(查找素数,筛选法)
题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...
- C#的cs文件中Response.Clear();Response.ClearHeaders()的作用
在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{ protected void Page_Load(object se ...
- 文件夹和文件、Path类、流、序列化
循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...