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
class Solution { | |
// 93. Restore IP Addresses | |
// https://leetcode.com/problems/restore-ip-addresses/ | |
public List<String> restoreIpAddresses(String rawIpString) { | |
List<String> restoredIps = new ArrayList<>(); | |
restoreIps(0, 0, new int[4], rawIpString, restoredIps); | |
return restoredIps; |
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
public class Solution { | |
// 79. Word Search | |
// https://leetcode.com/problems/word-search/ | |
public boolean exist(char[][] board, String word) { | |
for(int i = 0; i < board.length; i++) | |
for(int j = 0; j < board[0].length; j++){ | |
if (board[i][j] == word.charAt(0)) { | |
if(exist(board, i, j, word, 0)) |
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
public class Solution { | |
// 37. Sudoku Solver | |
// https://leetcode.com/problems/sudoku-solver/ | |
public void solveSudoku(char[][] board) { | |
if(board == null || board.length == 0) | |
return; | |
solve(board); | |
} | |
public boolean solve(char[][] board){ |
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
class Solution { | |
// 22. Generate Parentheses | |
// https://leetcode.com/problems/generate-parentheses/ | |
public List<String> generateParenthesis(int n) { | |
List<String> ans = new ArrayList(); | |
backtrack(ans, new StringBuilder(), 0, 0, n); | |
return ans; | |
} |
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
class Solution { | |
// 17. Letter Combinations of a Phone Number | |
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/ | |
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8109/My-recursive-solution-using-Java/222635 | |
private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; | |
public List<String> letterCombinations(String digits) { | |
List<String> ret = new LinkedList<String>(); |
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
class Solution { | |
// https://leetcode.com/problems/combination-sum-ii/ | |
// 40. Combination Sum II | |
public List<List<Integer>> combinationSum2(int[] candidates, int target) { | |
List<List<Integer>> list = new ArrayList<>(); | |
Arrays.sort(candidates); | |
boolean[] used = new boolean[candidates.length]; |
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
class Solution { | |
// 39. Combination Sum | |
// https://leetcode.com/problems/combination-sum/ | |
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; |
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
class Solution { | |
// https://leetcode.com/problems/subsets-ii/ | |
// 90. Subsets II | |
public List<List<Integer>> subsetsWithDup(int[] nums) { | |
List<List<Integer>> output = new LinkedList(); | |
boolean[] used = new boolean[nums.length]; | |
Arrays.sort(nums); | |
rec(nums, output, new ArrayList<>(), 0, used); |
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
class Solution { | |
// 78. Subsets | |
// https://leetcode.com/problems/subsets/ | |
public List<List<Integer>> subsets(int[] nums) { | |
List<List<Integer>> output = new LinkedList(); | |
rec(nums, output, new ArrayList<>(), 0); | |
// Arrays.sort(nums); | |
return output; |
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
public class Solution { | |
// https://leetcode.com/problems/permutations-ii/ | |
// 47. Permutations II | |
// https://leetcode.com/problems/permutations/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning) | |
public List<List<Integer>> permuteUnique(int[] nums) { | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
if(nums==null || nums.length==0) return res; | |
boolean[] used = new boolean[nums.length]; | |
List<Integer> list = new ArrayList<Integer>(); |
NewerOlder