
ACM-递归
文章平均质量分 76
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
POJ 1519 求大数各位上数字之和 大数处理 递归
这题很简单,但还是超时和WA一次主要是由于1、这题输入可能是非常大的数,可能上千位,所以把输入当成了2000位的字符串来处理,转化成数字2、还是变量初始化的问题,WA了一个,细心最重要#include #include using namespace std;int f (int n){ int sum = 0; while((n/10) > 0){ sum +=原创 2011-12-25 15:20:02 · 2661 阅读 · 0 评论 -
LeetCode Restore IP Addresses
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 m原创 2015-02-23 10:01:11 · 1927 阅读 · 0 评论 -
LeetCode Permutations
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].思路分析:这题是Permutations II问题的简原创 2014-11-23 13:01:17 · 2026 阅读 · 0 评论 -
LeetCode Permutations II
题目 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].思路分析:Pe原创 2014-11-23 12:56:52 · 2376 阅读 · 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 nei原创 2015-02-15 09:15:11 · 1931 阅读 · 0 评论 -
LeetCode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.N原创 2015-02-15 14:47:23 · 1733 阅读 · 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 of原创 2015-02-15 14:43:33 · 1566 阅读 · 0 评论 -
LeetCode Same Tree
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 same value.思路分析:判断两个树是否相同,基本也原创 2015-03-19 04:04:40 · 1592 阅读 · 0 评论 -
LeetCode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路分析:这题比较简单,关于树的题目通常都可以用递归解决,这题也不例外,递归解法的思原创 2015-03-19 03:50:24 · 1668 阅读 · 0 评论 -
LeetCode Minimum Depth of Binary Tree
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.思路分析:这题和Maximum Depth of Binary Tree类似,但是现原创 2015-03-19 13:25:30 · 1473 阅读 · 0 评论 -
LeetCode Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321"Give原创 2015-04-20 13:29:07 · 1503 阅读 · 0 评论 -
LeetCode Invert Binary Tree
这是最近比较火的一个题目,因为一条推特的转播“Google HR:我们90%的工程师都用你写的软件,但是你竟然不会在白板上面反转一颗二叉树,所以滚吧”,各方看法不一,但看这个题目,的确是一个很简单的题目。考察最基本的递归和树操作。下面给出了递归实现和借助栈的迭代实现(非递归实现)。后一个版本的可扩展性更好,可以处理更大的树。实在是容易题,就是DFS遍历一遍树节点,把每个树节点的左右孩子互换就可以了。或许是那个ios开发牛人不屑于准备就去Google面试,结果被爆,与其说是能力问题,不如说是态度问题。原创 2015-06-15 10:46:20 · 3750 阅读 · 1 评论 -
LeetCode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).思路分析:这题容易想到O(m+n)的解法,就是先Merge两个数组,然后返原创 2015-02-09 14:40:06 · 1666 阅读 · 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, given s = “catsanddo原创 2015-02-07 12:08:42 · 3795 阅读 · 0 评论 -
POJ 2499 求二叉树结点到根结点的路径长度 递归 二叉树
这题主要求二叉树结点到根结点的路径长度,基本的思路是 比较a与b,如果a大则当前结点是左孩子,a-b作为父结点的左数,父结点的右数与当前右数相等;如果b大则当前结点为右孩子,同理可以求父结点,直到父结点为(1,1)遍历结束。当用原始的递归算法会超时,需要考虑a=1或b=1的特殊情况,同时利用a与b的倍数关系加快遍历速度Source CodeProblem: 2499原创 2012-02-11 15:31:18 · 3242 阅读 · 0 评论 -
POJ 1664 求m个苹果放入n个盘子的不同放法数目 递归 分类讨论
通过分类讨论,将规模较大的问题转换成规模较小的相同问题,学会”降维“,将索引值不断降小,就可以递归求解设f(m,n)为把m个苹果放到n个盘子中的方法数,m>=0,n>=0.若m和n中任何一个等于0,那么f(m,n) = 1,注意不是等于0,因为相当于就那么一种结果,就是不往盘子里面放(没有苹果),或者,连盘子都没有。若n=1,显然对于任意的m>=0 有f(m,1) = 1若m=原创 2012-02-11 18:42:30 · 5078 阅读 · 2 评论 -
求全排列问题的算法
//全排列问题,近期面试的热门考题,收录于此/* 设R={r1,r2,...rn}是要进行排列的n个元素.Ri=R-{ri}.集合X中元素的全排列记为 Perm(X).(ri)Perm(X)表示在全排列Perm(X)的每一个排列前加上前缀ri得到的排列 R的全排列可归纳定义如下: 当n=1时,Perm(R)=(r),其中r是集合R中唯一的元素;转载 2012-05-10 22:51:47 · 6202 阅读 · 0 评论 -
LeetCode Validate Binary Search Tree
LeetCode Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys原创 2014-11-09 11:02:57 · 1894 阅读 · 0 评论 -
LeetCode Generate Parentheses
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:"((()))", "(()())", "(())()", "()(())", "()()()"思路分析:原创 2014-11-16 10:28:18 · 2141 阅读 · 0 评论 -
LeetCode Sort List
Sort a linked list in O(n log n) time using constant space complexity.思路分析:这题要求在O(n log n) 和常量空间对单链表排序,O(n log n) 的排序算法有快速排序,归并排序和堆排序,对于快速排序,其最坏情况下的时间复杂读是O(n),所以不符合要求。我们可以用归并排序解决这题。用归并排序的好处是平均时间和最坏时间都原创 2015-01-27 13:29:49 · 2169 阅读 · 1 评论 -
LeetCode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.思路分析:这题主要考察递归,我们可以画一个二叉树的实例来具体分析。比如如果postorder 是84526731,inorder是84521637,首先我们可以知道postorder的最后一个数1必然是root,然后我们可以在inorder里面找到这原创 2015-01-28 12:24:49 · 2290 阅读 · 1 评论 -
LeetCode Path Sum II
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 and sum = 22, 5 / \原创 2015-01-28 14:44:23 · 2238 阅读 · 2 评论 -
LeetCode Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum = 22原创 2015-01-28 13:46:02 · 1598 阅读 · 1 评论 -
LeetCode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路分析:这题类似于LeetCode Construct Binary Tree from Inorder and Postord原创 2015-01-28 12:30:19 · 2174 阅读 · 1 评论 -
LeetCode Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST is modifi原创 2015-07-19 15:11:29 · 3514 阅读 · 0 评论