代码还是这一块代码,但是还是写的很慢。。

其中用到了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排序)的更多相关文章

  1. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  2. 【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 ...

  3. 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) ...

  4. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  5. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  6. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  7. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. 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 ...

  9. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. python num[y array

    http://sebug.net/paper/books/scipydoc/numpy_intro.html npArr1=np.array([1,2,3],[4,5,6],[7,8,9]]) npA ...

  2. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  3. android重写view和viewgroup的区别

    重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...

  4. Ajax ContentType 列表

    ".*"="application/octet-stream" ".001"="application/x-001" & ...

  5. Ext学习-高级组件介绍

    在这一部分的学习中,主要是学习一些比较特殊的组件. 1.图表 2.日历 3.颜色,日期,时间的选择器 4.滑动条 5.各种工具类 参考文档:http://docs.sencha.com/extjs/4 ...

  6. [设计模式] 1/2 工程与抽象工程模式 factory & Abstrac Factory

    转载 http://blog.csdn.net/wuzhekai1985 软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多 ...

  7. 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法

    自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...

  8. jquery层居中,点击小图查看大图,弹出层居中代码

    1.层居中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  9. Longest Repeated Sequence【微软编程一小时-题目2】

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...

  10. Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...