随笔分类 - leetcode-DFS
摘要:Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens="catsanddog",dict=["cat", "cats", "and", "sand", "dog"].A
阅读全文
posted @ 2014-02-22 13:07
krunning
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]...
阅读全文
posted @ 2014-02-17 02:15
krunning
摘要:Given a collection of integers that might contain duplicates,S, return all possible subsets.IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []] 1 public class Solution{ 2 public ArrayList> subsetsWithDup(int[] num) { 3 Arrays.sort(num); 4 ArrayList> res = new Arra...
阅读全文
posted @ 2014-02-13 04:28
krunning
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter) 1 public class Solution { 2 public ArrayList restoreIpAddresses(S
阅读全文
posted @ 2014-02-10 15:32
krunning
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1]. 1 public class Solution { 2 public ArrayList> permuteUnique(int[] num) { 3 ArrayList> res = new Arra...
阅读全文
posted @ 2014-02-08 04:30
krunning
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ] 1 public class Solution { 2 public ArrayLis
阅读全文
posted @ 2014-02-06 15:01
krunning
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut. 1 public cla
阅读全文
posted @ 2014-02-06 15:00
krunning
摘要:Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []] 1 public class Solution { 2 ...
阅读全文
posted @ 2014-02-06 14:50
krunning
摘要:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.For example,Givenboard=[ [&q
阅读全文
posted @ 2014-02-06 14:39
krunning
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 ArrayList> res = new ArrayList>(); 4 ...
阅读全文
posted @ 2014-02-06 14:38
krunning
摘要:Now, instead outputting board configurations, return the total number of distinct solutions. 1 public class Solution { 2 public int totalNQueens(int n) { 3 int []total = new int [1]; 4 get(new int[n],0,n,total); 5 return total[0]; 6 } 7 public void get(int []queen...
阅读全文
posted @ 2014-02-06 14:21
krunning
摘要:public class Solution { public ArrayList solveNQueens(int n) { ArrayList res = new ArrayList(); dfs(n,0,new int[n],res); retur...
阅读全文
posted @ 2014-02-06 14:19
krunning
摘要:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1]. 1 public class Solution { 2 public ArrayList> permute(int[] num) { 3 int len = num.length; 4 ArrayList> res = ne...
阅读全文
posted @ 2014-02-06 14:16
krunning
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination. 1 public class Solution { 2 public ArrayList> combinationSum2(int[] candidates, int target) { 3 ...
阅读全文
posted @ 2014-02-06 14:13
krunning
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times. 1 public class Solution { 2 public ArrayList> combinationSum(int[] candidates, int target) {...
阅读全文
posted @ 2014-02-06 14:12
krunning
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'. 1 public class Solution { 2 public void solveSudoku(char[][] board) { 3 solve(board); 4 } 5 public boolean solve(char[][]board){ 6 for(int i=0;i<9;i++){ 7 ...
阅读全文
posted @ 2014-02-06 14:11
krunning
摘要:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()" 1 public class Solution { 2 public ArrayList generateParen
阅读全文
posted @ 2014-02-06 05:35
krunning
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q
阅读全文
posted @ 2014-02-06 05:30
krunning
浙公网安备 33010602011771号