Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 436ffc6

Browse files
author
C5141506
committed
leetcode problem no 40. Combination Sum II
1 parent 17612e7 commit 436ffc6

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package java_problem.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class CombinationSumII {
8+
public static void main(String args[]) {
9+
int[] nums = new int[]{10, 1, 2, 7, 6, 1, 5};
10+
System.out.println(new SolutionCom().combinationSum2(nums, 8));
11+
}
12+
}
13+
14+
class SolutionCom {
15+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
16+
Arrays.sort(candidates);
17+
List<List<Integer>> ans = new ArrayList<>();
18+
findCombiantions(0, candidates, target, ans, new ArrayList<>());
19+
return ans;
20+
}
21+
22+
void findCombiantions(int ind, int[] nums, int target, List<List<Integer>> ans, ArrayList<Integer> ds) {
23+
if (target == 0) {
24+
ans.add(new ArrayList<>(ds));
25+
return;
26+
}
27+
for (int i = ind; i < nums.length; i++) {
28+
if (i > ind && nums[i] == nums[i - 1]) continue;
29+
if (nums[i] > target) break;
30+
ds.add(nums[i]);
31+
findCombiantions(i + 1, nums, target - nums[i], ans, ds);
32+
ds.remove(ds.size() - 1);
33+
}
34+
}
35+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package java_problem.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class PermutationII {
8+
public static void main(String args[]) {
9+
int[] nums = new int[]{1,2,2};
10+
System.out.println(new SolutionII().subsetsWithDup(nums));
11+
}
12+
}
13+
14+
class SolutionII {
15+
16+
public List<List<Integer>> subsetsWithDup(int[] nums) {
17+
Arrays.sort(nums);
18+
List<List<Integer>> ans = new ArrayList<>();
19+
ans.add(new ArrayList<>());
20+
// for (int i = 0; i < nums.length; i++) {
21+
// List<Integer> list = new ArrayList<>();
22+
// subSet(nums, i, ans, list);
23+
// }
24+
subSet(nums, 0, ans, new ArrayList<>());
25+
26+
27+
return ans;
28+
}
29+
30+
public void subSet(
31+
int[] nums,
32+
int idx,
33+
List<List<Integer>> ans,
34+
List<Integer> list
35+
) {
36+
ans.add(new ArrayList<>(list));
37+
38+
for (int i = idx; i < nums.length; i++) {
39+
//skip the duplicate elements
40+
if (i > idx && nums[i] == nums[i - 1]) continue;
41+
list.add(nums[i]);
42+
subSet(nums, i + 1, ans, list);
43+
list.remove(list.size() - 1);
44+
}
45+
46+
// if (idx >= nums.length) {
47+
// if (!ans.contains(list)) {
48+
// ans.add(new ArrayList<>(list));
49+
// }
50+
//
51+
// return;
52+
// }
53+
//
54+
// list.add(nums[idx]);
55+
// if (!ans.contains(list)) {
56+
// ans.add(new ArrayList<>(list));
57+
// }
58+
//
59+
// subSet(nums, idx + 1, ans, list);
60+
// list.remove(list.size() - 1);
61+
// subSet(nums, idx + 1, ans, list);
62+
}
63+
}

‎src/main/problems/java_problem/backtracking/PermutationsApporoach2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
public class PermutationsApporoach2 {
7+
78
public static void main(String args[]) {
89
int[] nums = new int[]{1, 2, 3};
910
System.out.println(new PermutationsSolution().permute(nums));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//package java_problem.tree;
2+
//// class to create a node
3+
//class Node {
4+
// int data;
5+
// Node left, right;
6+
//
7+
// public Node(int data)
8+
// {
9+
// this.data = data;
10+
// left = right = null;
11+
// }
12+
//}
13+
//
14+
//public class TreeTopView {
15+
// // Driver code
16+
// public static void main(String[] args)
17+
// {
18+
// /* Create following Binary Tree
19+
// 1
20+
// / \
21+
// 2 3
22+
// \
23+
// 4
24+
// \
25+
// 5
26+
// \
27+
// 6
28+
// */
29+
// BinaryTree tree = new BinaryTree();
30+
// tree.root = new Node(1);
31+
// tree.root.left = new Node(2);
32+
// tree.root.right = new Node(3);
33+
// tree.root.left.right = new Node(4);
34+
// tree.root.left.right.right = new Node(5);
35+
// tree.root.left.right.right.right = new Node(6);
36+
// tree.TopView(tree.root);
37+
// }
38+
//}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /