Skip to content

Commit

Permalink
Add some solution
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed May 20, 2024
1 parent 11adde6 commit 46286a5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
29 changes: 29 additions & 0 deletions leetcode/problems/java/combination-sum-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
backtrack(result, new ArrayList<>(), candidates, target, 0);
return result;
}

void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] candidates, int remain, int start) {
if (remain < 0) {
return;
} else if (remain == 0) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1]) {
continue;
}
tempList.add(candidates[i]);
backtrack(result, tempList, candidates, remain - candidates[i], i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
}
27 changes: 27 additions & 0 deletions leetcode/problems/java/combination-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

class Solution {

public List<List<Integer>> combinationSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, target, 0);
return list;
}

private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int remain, int start) {
if (remain < 0)
return;
else if (remain == 0)
list.add(new ArrayList<>(tempList));
else {
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i);
tempList.remove(tempList.size() - 1);
}
}
}
}
28 changes: 28 additions & 0 deletions leetcode/problems/java/letter-combinations-of-a-phone-number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.List;
import java.util.ArrayList;

class Solution {
private String[] map = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits.length() == 0) {
return res;
}
backtrack(digits, 0, new StringBuilder(), res);
return res;
}

private void backtrack(String s, int start, StringBuilder sb, List<String> res) {
if (start == s.length()) {
res.add(sb.toString());
return;
}
String letters = map[s.charAt(start) - '0'];
for (int i = 0; i < letters.length(); i++) {
sb.append(letters.charAt(i));
backtrack(s, start + 1, sb, res);
sb.deleteCharAt(sb.length() - 1);
}
}
}
23 changes: 23 additions & 0 deletions leetcode/problems/java/sum-of-all-subset-xor-totals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {

public int subsetXORSum(int[] nums) {
return backtrack(nums, 0, 0);
}

int backtrack(int[] nums, int start, int xor) {
if (start >= nums.length)
return xor;
return backtrack(nums, start + 1, xor) + backtrack(nums, start + 1, xor ^ nums[start]);
}
}

class Solution2 {
public int subsetXORSum(int[] nums) {
int sum = 0;
int n = nums.length;
for (int num : nums) {
sum |= num;
}
return sum * (1 << (n - 1));
}
}

0 comments on commit 46286a5

Please sign in to comment.