-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11adde6
commit 46286a5
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
leetcode/problems/java/letter-combinations-of-a-phone-number.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |