随笔分类 - 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
阅读全文
摘要: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 =...
阅读全文
摘要:Given a collection of integers that might contain duplicates,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,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]解题思路: 跟subset 基...
阅读全文
摘要: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)ref:http://www.cnblogs.com/feiling/p/3301869.htmlpublic static Arr
阅读全文
摘要: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].参考:水中的鱼: [LeetCode]Permutations II解题报告[解题思路]跟 Permutations的解法一样,就是要考虑“去重”。先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否和自己相等,相
阅读全文
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.跟N-Queens 一个解发 最后return result.size()public class Solution{ public ArrayList solveNQueens(int n) { ArrayList result = new ArrayList(); int[] queenList = new...
阅读全文
posted @ 2014-02-07 07:54
Razer.Lu
摘要: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.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must
阅读全文
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be only one unique solution.A sudoku puzzle......and its solution numbers marked in red. 1 public class Solution { 2 public void solveSudoku(char[]
阅读全文
摘要: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
阅读全文
摘要: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.[Thoughts]凡是求
阅读全文
摘要: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"] ]【解题思路】DFS 找到s的所有的substring, 判断是否是palindrome,
阅读全文
摘要: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
阅读全文
摘要: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], []][解题思路]这个就是排列组合中的排列问题。递归如下逻辑:F...
阅读全文
摘要: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].Analysis:The idea of this classic problem is to use backtracking.We want to get permutations, which is mainly about swap values in th
阅读全文
摘要:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and'.&
阅读全文
摘要:[解题思路]典型的递归。一步步构造字符串。当左括号出现次数 generateParenthesis(int n) { ArrayList result = new ArrayList(); StringBuilder builder = new StringBuilder(); generate(result, builder, n, n); return result; } public void generate(ArrayList result, StringBuilder builder, int start,...
阅读全文

浙公网安备 33010602011771号