
算法-DFS
sparksnail
这个作者很懒,什么都没留下…
展开
-
POJ 1724 ROADS
题目总时间限制: 1000ms 内存限制: 65536kB 描述 N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be p原创 2018-01-03 17:25:50 · 216 阅读 · 0 评论 -
LintCode 16. Permutations II
题目思路DFS,因为要去重,所以需要判断一下每一个数字前面的数字是否和当前数字相同,以及是否被使用过。代码class Solution: """ @param: : A list of integers @return: A list of unique permutations """ def __init__(self):...原创 2018-02-16 22:56:31 · 251 阅读 · 0 评论 -
LintCode 33. N-Queens
题目 思路DFS,难点在斜线的判断。 1.当两个点在左上->右下的斜线时,x1−y1==x2−y2x1−y1==x2−y2x_1-y_1 == x_2-y_2 2.当两个点在右上->左下的斜线时,x1+y1==x2+y2x1+y1==x2+y2x_1+y_1 == x_2+y_2代码class Solution: """ @param:...原创 2018-02-16 23:31:26 · 221 阅读 · 0 评论 -
LintCode 121. Word Ladder II
题目 思路1.BFS,找到从end到start的路径,找到每一个单词的层数。 2.DFS,根据每一个单词的层数,按照层数递减的顺序遍历。代码import stringclass Solution: """ @param: start: a string @param: end: a string @param: dict: a ...原创 2018-02-17 02:15:35 · 1320 阅读 · 0 评论 -
LeetCode 140. Word Break II
题目思路DP+DFS 用dp[i]表示下标为i之前的字符都匹配上了,用来作为剪枝条件。代码class Solution(object): def __init__(self): self.res_list = [] def dfs(self, s, wordDict, startIndex, word_list, dp): if...原创 2018-04-01 01:17:53 · 143 阅读 · 0 评论 -
LeetCode 131. Palindrome Partitioning
题目思路dfs+dp。 判断一个字符串的所有子串是否为回文,可以用dp来加快计算。如果s[i]==s[j],dp[i][j]=dp[i+1][j−1]s[i]==s[j],dp[i][j]=dp[i+1][j−1]s[i]==s[j],dp[i][j]=dp[i+1][j-1]代码class Solution(object): def __init__(sel...原创 2018-04-02 15:09:10 · 134 阅读 · 0 评论 -
LeetCode 22. Generate Parentheses
题目思路dfs。用两个count记录左括号和右括号的数量,保证左括号数量大于等于右括号数量即可。代码class Solution: def __init__(self): self.res_list = [] def dfs(self, nums, n, count1, count2): if len(nums) ==...原创 2018-04-17 01:16:53 · 138 阅读 · 0 评论 -
LeetCode 17. Letter Combinations of a Phone Number
题目思路DFS代码class Solution: def __init__(self): self.res_list = [] def dfs(self, digits, index, map_dict, tmp_list): if index == len(digits): self.res_li...原创 2018-05-02 23:43:11 · 167 阅读 · 0 评论 -
LeetCode 39. Combination Sum
题目思路dfs代码class Solution: def __init__(self): self.res_list = [] def dfs(self, candidates, num, target, startIndex): if target < 0: return if target == ...原创 2018-05-06 00:26:07 · 132 阅读 · 0 评论 -
LeetCode 40. Combination Sum II
题目思路dfs,注意去重即可。代码class Solution: def __init__(self): self.res_list = [] def dfs(self, candidates, target, num, startIndex): if target < 0: return if ta...原创 2018-05-06 00:36:12 · 148 阅读 · 0 评论 -
剑指offer 字符串的排列
题目输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 输入描述: 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。思路dfs代码# -*- coding:utf-8 -*-class Solution: def _...原创 2018-06-20 21:29:08 · 148 阅读 · 0 评论 -
LintCode 15. Permutations
题目思路DFS代码1(递归)class Solution: """ @param: nums: A list of integers. @return: A list of permutations. """ def __init__(self): self.used = {} self.res_l...原创 2018-02-16 22:01:43 · 343 阅读 · 0 评论 -
LintCode 136. Palindrome Partitioning
题目思路DFS代码class Solution: """ @param: s: A string @return: A list of lists of string """ def __init__(self): self.res_list = [] def isPalindrome(self, s):...原创 2018-02-16 21:30:27 · 243 阅读 · 0 评论 -
深度优先搜索
深搜:对每个可能的路径深入到不能够再深入为止。 程序框架bool Dfs(v){ if(v为终点) return true; if(v为旧点) return false; 将v标记为旧点; 对和v相邻的每个结点U{ if(Dfs(U) == true) return true; }原创 2017-12-29 10:22:24 · 212 阅读 · 0 评论 -
POJ 2815 城堡问题
题目总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####—#####—#—#####—# 2 # # | # # # # # #—#####—##原创 2017-12-29 14:49:42 · 454 阅读 · 0 评论 -
LeetCode 51 N-Queens
题目The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2018-01-10 15:08:45 · 218 阅读 · 0 评论 -
POJ 4123:马走日
题目总时间限制: 1000ms 内存限制: 1024kB 描述 马在中国象棋以日字形规则移动。 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点。 输入 第一行为整数T(T 每一组测试数据包含一行,为四个整数,分别为棋盘的大小以及初始位置坐标n,m,x,y。(0输出 每组测试数据包含一行原创 2018-01-10 16:09:20 · 1093 阅读 · 0 评论 -
LintCode 17. 子集
题目给定一个含不同整数的集合,返回其所有的子集 注意事项 子集中的元素排列必须是非降序的,解集必须不包含重复的子集 您在真实的面试中是否遇到过这个题? Yes 样例 如果 S = [1,2,3],有如下的解: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 挑战原创 2018-02-06 22:00:17 · 329 阅读 · 0 评论 -
LintCode 18. 带重复元素的子集
题目给定一个可能具有重复数字的列表,返回其所有可能的子集 注意事项 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 您在真实的面试中是否遇到过这个题? Yes 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]原创 2018-02-06 22:09:27 · 357 阅读 · 0 评论 -
LintCode 69. Binary Tree Level Order Traversal
题目思路challenge1:BFS challenge2:DFS代码1"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""class ...原创 2018-02-13 14:56:32 · 289 阅读 · 0 评论 -
LintCode 135. Combination Sum
题目思路DFS代码class Solution: """ @param: candidates: A list of integers @param: target: An integer @return: A list of lists of integers """ def __init__(self): ...原创 2018-02-16 11:39:54 · 271 阅读 · 0 评论 -
LintCode 153. Combination Sum II
题目思路DFS,把结果加入到返回list的时候注意去重。代码class Solution: """ @param: num: Given the candidate numbers @param: target: Given the target number @return: All the combinations that sum ...原创 2018-02-16 11:48:39 · 230 阅读 · 0 评论 -
剑指offer 机器人的运动范围
题目地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?思路dfs代码# -*-...原创 2018-06-13 22:37:50 · 150 阅读 · 0 评论