
Backtracking
iteye_17352
这个作者很懒,什么都没留下…
展开
-
Leetcode - Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers ...原创 2015-06-10 10:09:15 · 96 阅读 · 0 评论 -
Leetcode - Subset
[分析] 三种思路思路1:每层递归新加一个元素,第一层递归,在结果中加入空集,然后循环添加不同的第一个元素并向下递归;第二层递归,先将输入参数中包含一个元素的子集加入结果,然后循环添加不同的第二个元素并向下递归……第 n + 1层将包含 n个元素的全集加入结果。思路2和思路3参考Code Ganker博客,分别是递归和迭代的思路,个人觉得迭代更好理解。 [ref] subset: ...原创 2015-08-02 12:06:51 · 183 阅读 · 0 评论 -
Leetcode - Subset II
[分析] 延续Subset三种思路,关键是添加去重处理思路1:仅需在递归函数循环前面的加个if判断,这个技巧在Combination,Permutation中均使用。这个去重处理是三种实现中最简洁的,不容易出错。思路2和思路3参考Code Ganker博客,分别是递归和迭代的思路,去重处理花了番功夫理解。[ref]subset II: [url]http://blog.csdn....原创 2015-08-02 12:13:11 · 134 阅读 · 0 评论 -
Leetcode - Word Search
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 n...原创 2015-08-03 21:03:49 · 105 阅读 · 0 评论 -
Leetcode - Word Search II
iven a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those hori...原创 2015-08-03 21:25:55 · 112 阅读 · 0 评论 -
Leetcode - Generate Parentheses
[分析] 第一个思路(错误的~):假设递归函数返回 n - 1对括号组成的所有合法字符串,将其看做整体A,则 所求 n 对括号组成的合法字符串是所有()A,A(),(A)。这个思路对于n 4,就会漏解,比如对于 n=4, 会漏掉(())(())第二个思路:递归构造出合法字符串,初始时拥有 n 个左右括号,递归的每一层根据当前资源情况添加一个左括号或者右括号后继续递归,递归结束条件是 n 个...原创 2015-08-08 17:01:30 · 89 阅读 · 0 评论 -
Leetcode - Factor Combination
Numbers can be regarded as product of its factors. For example,8 = 2 x 2 x 2; = 2 x 4.Write a function that takes an integer n and return all possible combinations of its factors.Note: ...原创 2015-08-28 09:53:20 · 125 阅读 · 0 评论 -
Leetcode - Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.For example:Given s = "aabb", return ["ab...原创 2015-08-28 21:17:29 · 150 阅读 · 0 评论 -
Leetcode - Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "cats...原创 2015-04-14 09:59:43 · 111 阅读 · 0 评论 -
Leetcode - WordBreak III
Given a string s and a dictionary of words dict, determine if s can be segmented into a sequence of one or more dictionary words. But notice that each word in the dictionary can be used at most ...原创 2015-04-16 08:30:27 · 167 阅读 · 0 评论 -
Leetcode - Gray Code
原题链接:[url]https://leetcode.com/problems/gray-code/[/url][分析] 这题更像是找规律,n = 1时是 0, 1, n = 2时是00,01, 11, 10,后面两项可以看做是先对 0,1镜像得到1,0,然后分别加上10得到11,10, 因此可以从 i = 1时迭代求得 i = n的结果。Method2 是对 Method1的代码优化,空间使...原创 2015-08-01 17:26:24 · 134 阅读 · 0 评论 -
Leetcode - Permutation Sequence
原题链接:[url]https://leetcode.com/problems/permutation-sequence/[/url][分析] 思路1:调用 k 次NextPermutation.思路2:数学解法,在n!排列中,每个第一位元素带领 (n-1)! 个排列数,假设 p = k / (n-1)!,则num[p]就是第一位上的数字,注意 k 要从0开始计数,因此进主循环前k--...原创 2015-08-01 17:19:47 · 113 阅读 · 0 评论 -
Leetcode - Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must e...原创 2015-06-26 09:19:26 · 129 阅读 · 0 评论 -
Leetcode - N-Queens
[分析] N皇后摆放规则:两个皇后不能共存于同一行、同一列以及同一(斜率为1的)斜线上。rows 数组用于记录每行的皇后所摆放的列位置,并用于构造结果;cols数组记录该列为哪个皇后的地盘,两个数组共同完成回溯过程。[code="java"]public class Solution { public List solveNQueens(int n) { i...原创 2015-07-30 20:38:49 · 96 阅读 · 0 评论 -
Leetcode - N Queues II
[分析] 做完N皇后第一题,这个就so easy~[code="java"]public class Solution { public int totalNQueens(int n) { int[] rows = new int[n]; // rows[i] = j, means board[i][j] = Q int[] cols = ...原创 2015-07-30 20:52:15 · 85 阅读 · 0 评论 -
Leetcode - Sudoku Solver
[分析] 做Valid Sudoku时表示3*3区块的下标想得很纠结,其实动手把81个格子的坐标都写出来,左上角为(0,0),这个过程中就能发现坐标规律,越勤奋会越聪明~ 这道题目虽然被判为Hard,但看到答案后其实并不难,回溯的题目是有模式可循的,虽然我现在还不是很熟练,但多加练习一定能掌握。早上看到一大神的话,能5天刷一遍leetcode时再问咋还不熟呢,与大家共勉~ 另看参考Code_Ga...原创 2015-07-31 09:14:45 · 108 阅读 · 0 评论 -
Leetcode - Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number o...原创 2015-07-31 20:21:33 · 82 阅读 · 0 评论 -
Leetcode - Combination Sum II
[分析] 输入数组中的每个元素至多使用一次,相较于Combination Sum,需要做两个小改动,改动虽小但很关键。改动1是在向下递归时传下去的start是下一个元素的下标,这保证每个元素至多只被使用一次。改动2是for循环体前面的if判断,这个判断的作用是避免出现重复结果,虽然每个元素不能被重复使用,但重复出现在结果中是允许的(考虑[1,1], 2),因此限定只对第一次得到某个数进行递归(i ...原创 2015-07-31 21:06:29 · 78 阅读 · 0 评论 -
Leetcode - Combination Sum III
[分析] 思路就是枚举k个数所有可能的组合并判断是否符合条件。递归时需要如下状态量:k——还可以添加多少个数字 ,gap——和目标之前的差距, base——本次递归可使用的最小数,item——正在构造的一个解,result——全部解。[code="java"]public class Solution { public List combinationSum3(int k, ...原创 2015-07-31 22:04:19 · 90 阅读 · 0 评论 -
Leetcode - Combination
[分析] 从 n 个数中取 k 个数,第一个数有 n 种取法……第 k 个数有 n - (k - 1)种取法,共需 k 层递归,第 i 层递归设置组合中第 i 个数的值。[code="java"]public class Solution { public List combine(int n, int k) { List result = new Arra...原创 2015-08-01 08:36:19 · 80 阅读 · 0 评论 -
Leetcode - Permutation II
原题链接:[url]https://leetcode.com/problems/permutations-ii/[/url][分析] 同Permutation一题的区别在于输入数组可能包含重复元素,在面试时即使面试官没有明确指出也该想到。如何处理重复元素不致得到重复结果呢?基本思路是排序然后跳过重复元素。Method1 每次循环递归都要排序且要复制一次数组,时间和空间消耗都比较大,Method...原创 2015-08-01 10:49:38 · 139 阅读 · 0 评论 -
Leetcode - Palindrome Partition
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa...原创 2015-05-21 09:56:20 · 163 阅读 · 0 评论