Skip to content

Instantly share code, notes, and snippets.

@anil477
anil477 / combination.java
Created February 16, 2021 12:47
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
// https://leetcode.com/problems/combination-sum/
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
@anil477
anil477 / TransitiveClosure.java
Created July 25, 2017 04:52
Transitive Closure of a Graph using DFS
// Given a directed graph, find out if a vertex v is reachable from another vertex u for all vertex pairs (u, v) in the given graph.
import java.util.LinkedList;
class Graph
{
int V; // No. of vertices
boolean [][]tc; // To store transitive closure
LinkedList<Integer> adj[]; // array of adjacency lists
public Graph(int V)
@anil477
anil477 / RestoreIPAddresses.java
Created January 30, 2022 14:46
93. Restore IP Addresses
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;
@anil477
anil477 / Word Search.java
Created January 30, 2022 09:17
79. Word Search
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))
@anil477
anil477 / SudokuSolver.java
Created January 30, 2022 08:48
37. Sudoku Solver
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){
@anil477
anil477 / GenerateParentheses.java
Created January 30, 2022 06:53
22. Generate Parentheses
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;
}
@anil477
anil477 / LetterCombinationsofPhoneNumber.java
Created January 29, 2022 19:48
17. Letter Combinations of a Phone Number
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>();
@anil477
anil477 / CombinationSumII.java
Created January 29, 2022 14:05
40. Combination Sum II
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];
@anil477
anil477 / CombinationSum.java
Created January 29, 2022 13:58
39. Combination Sum
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;
@anil477
anil477 / SubsetsII.java
Created January 29, 2022 12:28
90. Subsets II
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);