递归和回溯
ojshilu
https://github.com/lucky521
展开
-
把递归转变成循环
1、时间性能:递归的逻辑,使用递归来设计程序,会使得程序的时间较差。因此在oj的时候最好改成备忘录的方式来记录递归。2、数据表示范围:在测试数据时一定要记得用超大数据测试一下,看数据是否可能发生溢出。对于32位无法存储的大整数,最好使用__int64。输出的时候格式为%I64d。(注意这里的I是大写的i)。hduoj-2044:斐波那契的简单变形。斐波那契数列到后来增长速度还是很快原创 2013-09-23 00:23:00 · 1489 阅读 · 0 评论 -
字符串单词拆分 Word Break
问题:给出一个字符串,问能否将其拆分为1或多个单词。单词表已给出。Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given原创 2014-03-17 10:07:28 · 3426 阅读 · 0 评论 -
变形字符串识别 Scramble String
问题:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \原创 2014-03-27 19:51:42 · 1649 阅读 · 0 评论 -
计算1~n之间的所有十进制整数中1的出现次数
问题是这样的,给出一个n,让你求出1~n之间的所有整数的十进制形式中的1的总个数。本问题假设n>1。比如给出一个数n=12,那么1到12之间的数1,2,3,4,5,6,7,8,9,10,11,12里面,能数出来是有5个1。最直观的方法:依次判断每一个区间内的每一个整数,对一个数逐渐除10判断其每一位。这个方法的时间复杂度为O(n*lgn)。介绍一个优化的方法(注意其中分治和原创 2013-12-03 10:49:36 · 1674 阅读 · 1 评论 -
回文子串划分 Palindrome Partitioning
问题:将一个字符串进行划分,要求划分成的子串都必须是回文串。找出所有可能的划分情况。思路:backstrack吧,每一次只有在满足是回文的情况下深入下一层。class Solution {public: vector> partition(string s) { vector > result; vector now; if(s.s原创 2014-03-21 19:21:53 · 1431 阅读 · 0 评论 -
组合生成 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 of原创 2014-02-24 10:59:35 · 912 阅读 · 0 评论 -
求二叉树的最小深度 Minimum Depth of Binary Tree
题目源自于leetcode。题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:分情况讨论,没有孩子的结点是叶子原创 2013-11-19 16:36:35 · 3498 阅读 · 0 评论 -
根据有序数组构造二叉查找树 Convert Sorted Array to Binary Search Tree
题目源自于leetcode。题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:注意这里要的是平衡的BST。递归思想+二分查找法。把正中间的数作为树根,然后左和右两半段作为左子树和右子树。代码:/** * Definit原创 2013-11-08 21:29:21 · 1431 阅读 · 0 评论 -
求二叉树的最大深度 Maximum Depth of Binary Tree
题目源自于Leetcode。简单递归题,是Leetcode最简单的一道题。代码:class Solution {public: int maxDepth(TreeNode *root) { if(root == NULL) return 0; else { i原创 2013-11-06 13:51:01 · 2033 阅读 · 0 评论 -
根据单链表构造二叉查找树 Convert Sorted List to Binary Search Tree
问题:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:二叉查找树的构造往往伴随着二分查找的过程。这是一个单链表而不是数组。二分查找不好用。就用递归吧。每次找到链表的最中间结点, 作为树根节点,左右两侧作为左右孩子。代原创 2014-03-07 10:58:10 · 1338 阅读 · 0 评论 -
全排列生成 Permutations
题目源自于leetcode。题目: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].原创 2013-11-13 18:44:50 · 1512 阅读 · 0 评论 -
寻找两个不等长数组的中位数 Median of Two Sorted Arrays
题目源自于Leetcode。经典好题。题目:给出两个有序数组,长度不一定相同,一个是m一个是n,要求给出他们合并在一起之后的数组的中位数。要求时间复杂度为O(log(m+n)),所以不可以合并数组再找中位数,否则复杂度就是O(m+n)。数组的中位数如果有序数组有奇数个元素,那么中位数是最中间那个元素。如果有序数组有偶数个元素,那么中位数是中间两个元素的平均数。原创 2013-11-10 14:42:02 · 4886 阅读 · 1 评论 -
生成一个集合的所有子集 Subset
典型的递归状态生成问题。类似于全排列的生成问题。题目:Given a set of distinct integers, S, return all possible subsets.思路:借助一个只含0和1的数组来表示存在状态。不断切换状态进行深层递归,在递归最深处生成子集。代码:class Solution {public: vector > subset原创 2014-02-18 22:06:55 · 2954 阅读 · 0 评论 -
卡特兰数在多种问题下的应用 组合数学-Catalan数
卡特兰数是组合数学中常用的一个数列:通项公式为:h(n) = C(2n, n)/(n+1)递推公式为:h(0)=1,h(1)=1,h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) ,(n>=2)。从卡特兰数的递推公式来看,这是一个关于统计划分情况的递归问题。Catala原创 2013-11-09 18:12:07 · 3615 阅读 · 0 评论 -
根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
9oj编号1385。题目:根据二叉树的前序遍历序列和中序遍历序列,求得该二叉树的后序遍历序列。注意:有个前提,二叉树中的所有元素都不相同。方法:根据前序序列和中序序列重建二叉树原型,然后进行后序遍历。所以关键就在于重建二叉树。使用递归的方法比较容易。如下图所示,对于二叉树的构建过程也是递归的。首先把当前前序遍历序列的第一个元素作为root结点,并在在中序遍历中找原创 2013-09-20 22:18:02 · 1214 阅读 · 0 评论 -
N皇后问题的局面个数 N-Queens II
对于N皇后问题,有多少种可能的局面呢?温习一下backtrack吧。代码:class Solution {public: bool isValid(int board[], int n, int row, int col) { for(int i=0;i<row;i++) //判断当前位置(row, col)是否和之前行的元素有冲突原创 2014-03-09 16:07:38 · 1230 阅读 · 0 评论 -
手机键盘上的排列组合问题 Letter Combinations of a Phone Number
题目源自于Leetcode。题目: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.思路:原创 2013-10-20 17:25:50 · 1388 阅读 · 0 评论 -
只用递归来判断括号是否匹配
题目来自于待字闺中的微信。要求:对一个字符串进行括号匹配,要求是使用递归,不可以使用任何循环语句。如"a(bc((def)(gh))i)j"思路:1、一般来讲,递归和循环是可以互相转换的。不允许使用循环,就要用递归结构来实现循环。2、括号匹配一般是使用栈结构。栈和递归又有很紧密的联系。下面是我的实现方法#include#define OK 1#def原创 2013-09-15 15:50:50 · 3497 阅读 · 0 评论 -
生成所有括号匹配情况 Generate Parentheses
题目源自于leetcode。题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()",原创 2013-10-28 11:08:00 · 1391 阅读 · 0 评论 -
1390 小矩形覆盖
1,递归方法超时2,int型存储溢出3,Output Limit Exceeded错误#include#includeusing namespace std;unsigned long a[71];int fun(int n){ for(int i=1;i<=n;i++) if(i == 1) a[i] = 1; else if(i ==2)原创 2013-07-12 17:50:02 · 850 阅读 · 0 评论 -
简单的正则表达式匹配 Regular Expression Matching
题目源自于Leetcode。只需要支持两个匹配符*和.。'.' Matches any single character.'*' Matches zero or more of the preceding element.本题的要求是能够全部匹配整个母字符串,而不是包含有。The matching should cover the entire input string (no原创 2013-10-13 22:41:33 · 2387 阅读 · 0 评论 -
判断两棵树是一模一样的 Same Tree
题目源自于Leetcode。简单的递归题。题目:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the原创 2013-11-06 14:05:49 · 1351 阅读 · 0 评论 -
按身高排成两队问题 catalan数
题目源自于阿里巴巴校招笔试题。题目:12个高矮不同的人,排成2排,每排必须是从矮到高排列,而且第二排比对应的第一排的人高,问排列方式有多少种?思路:假设12个人的身高为,0 1 2 3 4 5 6 7 8 9 10 11。我们先把这12个人从低到高排列,然后,选择6个人排在第一排,那么剩下的6个肯定是在第二排。用0表示对应的人在第一排,用1表示对应的人在第二排。那么含有原创 2013-11-04 19:16:00 · 1878 阅读 · 0 评论 -
平衡二叉树判定 AVL Balanced Binary Tree
题目源自于leetcode。题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every原创 2013-11-11 14:43:14 · 1731 阅读 · 0 评论 -
Sum Root to Leaf Numbers
题目源自于leetcode。二叉树递归算数题。题目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the nu原创 2013-12-06 19:43:28 · 829 阅读 · 0 评论 -
Restore IP Addresses
Leetcode问题: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原创 2014-03-02 11:50:29 · 844 阅读 · 0 评论 -
八皇后问题 EightQueen
题目:在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即要求任意两个皇后不得处在同一行、同一列或者同一对角斜线上。求出总共有多少种摆法。思路: 对于不在同行且不在同列的摆法,8行每行各有一个皇后,那么就是在8列中进行全排列,很明显有8!种情况。在这8!中情况中,再排除对角斜线上在出现冲突的情况。 对于排列组合类型的问题,经常采用递归方式来解决。数据结构:因为每行原创 2013-11-01 20:01:34 · 2139 阅读 · 0 评论 -
生成N皇后问题所有局面 N-Queens
Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("ba原创 2014-03-10 19:25:17 · 950 阅读 · 0 评论 -
数组的最大子数组和 Maximum Subarray
题目源自于Leetcode。题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subar原创 2013-11-08 15:05:52 · 1296 阅读 · 0 评论