Leetcode#40Combination Sum II

Combination Sum II

Total Accepted: 34820 Total Submissions: 138493My Submissions

Question Solution

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 (a1, a2, … , 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]

使用堆栈做的,超级不好的方法,没办法啊争取改进

public class Solution {

List<List<Integer>> x=new ArrayList<List<Integer>>();

Map<String, Integer> m=new HashMap<String, Integer>();

void output(Stack<Node> g){

Stack<Node> s=new Stack<Node>();

int[] data=new int[g.size()];

int i=0;

List<Integer> v=new ArrayList<Integer>();

while(!g.isEmpty()){

Node d=g.peek();

//v.add(d.val);

data[i]=d.val;

g.pop();

s.push(d);

i++;

}

while(!s.isEmpty()){

Node d=s.peek();

s.pop();

g.push(d);

}

for(int j=1;j<g.size();j++)

for(int jj=j;jj>0;jj--)

{

if(data[jj]<data[jj-1])

{

int mid=data[jj];

data[jj]=data[jj-1];

data[jj-1]=mid;

}

}

String str=Integer.toString(data[0]);

v.add(data[0]);

for(int j=1;j<g.size();j++)

{

v.add(data[j]);

str=str+" "+Integer.toString(data[j]);

}

if(m.get(str)==null)

{

x.add(v);

m.put(str,1);

}

}

public List<List<Integer>> combinationSum2(int[] candidates, int target) {

Stack<Node> stack=new Stack<Node>();

int l=candidates.length;

int i=0;

while(i<l)

{

if(candidates[i]==target)

{

//List<Integer> v=new ArrayList<Integer>();

//v.add(candidates[i]);

//x.add(v);

Node n=new Node(candidates[i], i);

stack.push(n);

output(stack);

i++;

stack.pop();

}

else if(candidates[i]>target)

{

i++;

}

else

{

break;

}

}

if(i==l)

return x;

Node z=new Node(candidates[i],i);

stack.push(z);

int sum=target-z.val;

while(!stack.isEmpty()){

Node p=stack.peek();

int c=p.cur;

if(sum==0)

{

output(stack);

Node u=stack.peek();

stack.pop();

sum=sum+u.val;

if(u.cur<l-1)

{

int h=u.cur;

while(h+1<l)

{

if(candidates[h+1]<=sum)

break;

else

h++;

}

if(h+1<l)

{

Node f=new Node(candidates[h+1],h+1);

stack.push(f);

sum=sum-candidates[h+1];

}

else

{

while(!stack.isEmpty())

{

u=stack.peek();

stack.pop();

sum=sum+u.val;

int hh=u.cur;

while(hh+1<l)

{

if(candidates[hh+1]<=sum)

break;

else

hh++;

}

if(hh+1<l)

{

Node f=new Node(candidates[hh+1],hh+1);

stack.push(f);

sum=sum-candidates[hh+1];

break;

}

}

}

}

else

{

while(!stack.isEmpty())

{

u=stack.peek();

stack.pop();

sum=sum+u.val;

int h=u.cur;

while(h+1<l)

{

if(candidates[h+1]<=sum)

break;

else

h++;

}

if(h+1<l)

{

Node f=new Node(candidates[h+1],h+1);

stack.push(f);

sum=sum-candidates[h+1];

break;

}

}

}

}

else

{

while(c+1<l)

{

if(candidates[c+1]<=sum)

break;

else

c++;

}

if(c+1<l)

{

Node f=new Node(candidates[c+1],c+1);

stack.push(f);

sum=sum-candidates[c+1];

}

else

{

while(!stack.isEmpty())

{

Node u=stack.peek();

stack.pop();

sum=sum+u.val;

if(u.cur<l-1)

{

int h=u.cur;

while(h+1<l)

{

if(candidates[h+1]<=sum)

break;

else

h++;

}

if(h+1<l)

{

Node f=new Node(candidates[h+1],h+1);

stack.push(f);

sum=sum-candidates[h+1];

break;

}

}

}

}

}

}

return x;

}

}

class Node{

int val;

int cur;

Node(int c1, int c2){

val=c1;

cur=c2;

}

}

时间: 2024-12-31 20:13:33

Leetcode#40Combination Sum II的相关文章

LeetCode: Combination Sum II [039]

[题目] 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

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II [113]

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [题意] 判断二叉树中是否存在一条从根到

LeetCode: Combination Sum II 解题报告

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 ta

Leetcode#113Path Sum II

Path Sum II Total Accepted: 43473 Total Submissions: 162906My Submissions Question Solution Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22,  

LeetCode: Path Sum II 解题报告

Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] SOLUTION 1: 使用

[LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 mu

[Leetcode] Path Sum II路径和

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree andsum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

[leetcode]Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 分析: 这道题目算是Path Sum的延伸,只不过