博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Combination Sum II
阅读量:4074 次
发布时间:2019-05-25

本文共 1416 字,大约阅读时间需要 4 分钟。

Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

Java代码:

public class Solution {    public  List
> combinationSum2(int[] candidates, int target) { List
> finalList = new ArrayList
>(); Arrays.sort(candidates); comb (candidates,0, target, finalList, new ArrayList
()); return finalList; } public void comb (int[] candidates,int num, int target, List
> finalList, ArrayList
array){ if(target==0){ if(!finalList.contains(array)){ // No repeat solutions finalList.add(array); return; } } if(target<0){ // solutions not accurate return; } for(int i = num;i< candidates.length;i++){ if(candidates[num]>target){ continue; } ArrayList
newArray = (ArrayList
) array.clone(); newArray.add(candidates[i]); if(i<=candidates.length){ comb(candidates,i+1,target-candidates[i],finalList,newArray); } // recursion to check the rest of the list } return; }}
 

转载地址:http://ypuni.baihongyu.com/

你可能感兴趣的文章
HOLOLENS程序发布,这个界面调用的图片
查看>>
看出在玩啥了吗?想不想体验下
查看>>
Time.deltaTime 的平均值在0.1-0.2左右
查看>>
向量加减法运算及其几何意义
查看>>
magnitude是精确距离,sqrMagnitude是节省CPU的粗略距离,精度有损失
查看>>
学习和研究下unity3d的四元数 Quaternion
查看>>
一些最最基本的几何图形公式
查看>>
Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
查看>>
理解四元数的一些文章
查看>>
Unity Shader入门
查看>>
片元着色器(Fragment Shader)被称为像素着色器(Pixel Shader),但
查看>>
UNITY自带的3D object没有三角形?
查看>>
Lambert(朗伯)光照模型 和Half Lambert的区别
查看>>
float4数据类型
查看>>
【Unity Shaders】学习笔记
查看>>
Holographic Remoting Player
查看>>
unity之LOD
查看>>
UNITY 移动到指定位置的写法
查看>>
Unity中关于作用力方式ForceMode的功能注解
查看>>
UNITY实现FLASH中的setTimeout
查看>>