
递归
文章平均质量分 63
张荣华_csdn
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
1-N整数中1出现的次数
如果不希望计算每个数字的1的个数,那就只能去寻找1在数字中出现的规律了。为了找到规律,不妨用一个稍微大一点的数字如21345作为例子来分析。我们把1-21345的所有数字分为两段:一段时1-1345,另一端是1346-21345.我们先看1346-21345中1出现的次数。1的出现分成两种情况。首先分析1出现在最高位的情况。在1346-21345的数字中,1出现在10000-19999这10000...原创 2018-06-02 14:02:45 · 949 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections...原创 2018-06-29 10:59:50 · 367 阅读 · 0 评论 -
找零钱问题系列之暴力搜索
有数组penny,penny中所有的值都为正数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim(小于等于1000)代表要找的钱数,求换钱有多少种方法。给定数组penny及它的大小(小于等于50),同时给定一个整数aim,请返回有多少种方法可以凑成aim。测试样例:[1,2,4],3,3返回:2方法一:暴力搜索class Exchange {public: ...原创 2018-06-24 00:16:16 · 605 阅读 · 0 评论 -
找零钱问题系列之记忆搜索
有数组penny,penny中所有的值都为正数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim(小于等于1000)代表要找的钱数,求换钱有多少种方法。给定数组penny及它的大小(小于等于50),同时给定一个整数aim,请返回有多少种方法可以凑成aim。测试样例:[1,2,4],3,3返回:2class Exchange {public: int cou...原创 2018-06-24 00:16:23 · 343 阅读 · 0 评论 -
Sum Root to Leaf Numbers
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 number 123.Find the total su...原创 2018-06-28 17:23:38 · 191 阅读 · 0 评论 -
Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re...原创 2018-06-29 09:57:08 · 206 阅读 · 0 评论 -
字符串的序列
题目:输入一个字符串,打印出该字符串中字符的所有排列。void Permutation(char* pStr, char* pBegin);void Permutation(char* pStr){ if(pStr == nullptr) return; Permutation(pStr, pStr);}void Permutation(char* pStr, char*...原创 2018-07-15 00:55:25 · 1286 阅读 · 0 评论 -
Letter Combinations of a Phone Number(C++)
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given...原创 2018-05-23 13:14:49 · 472 阅读 · 0 评论 -
Permutations
Given a collection of distinct integers, return all possible permutations.class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector&...原创 2018-05-26 11:40:10 · 353 阅读 · 0 评论 -
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.class Solution {public: vector<vector<int>> permuteUnique(vector<int>& num...原创 2018-05-26 11:55:09 · 241 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]class Solution {p...原创 2018-06-19 01:12:06 · 224 阅读 · 0 评论 -
递归查找数组的最大值
题目:用递归的方法实现查找数组中的最大值。#include "stdio.h"int getMaxValue(int *k,int n){ int tmp; if(1 == n) { return k[0]; } else { tmp = getMaxValue(k+1,n-1); } if(...原创 2018-07-28 00:10:24 · 1051 阅读 · 0 评论 -
1-n整数中1出现的次数
题目:输入一个整数n,求1-n这n个整数的十进制表示中1出现的次数。例如,输入12,1-12这些整数中包含1的数字有1、10、11、12,1一共出现了5次。采用递归的思路int NumberOf1(const char* strN);int PowerBase10(unsigned int n);int NumberOf1Between1AndN_Solution2(int n){...原创 2018-07-18 00:03:53 · 431 阅读 · 0 评论 -
Reverse Linked List(递归解法)
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULL/** * Definition for singly-linked list. * struct ListNode { * in...原创 2018-08-02 00:26:44 · 663 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree(递归法)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...原创 2018-08-07 00:05:03 · 608 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between ...原创 2018-08-07 00:04:56 · 209 阅读 · 0 评论 -
字符串通配符(递归)
题目描述问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。要求:实现如下2个通配符:*:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)?:匹配1个字符输入:通配符表达式;一组字符串。输出:返回匹配的结果,正确输出true,错误输出false#include <iost...原创 2018-08-12 10:53:16 · 1076 阅读 · 0 评论 -
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.Note: A leaf is a node with no children.Ex...原创 2018-06-26 09:05:33 · 181 阅读 · 0 评论 -
Balanced Binary Tree
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 node never differ by...原创 2018-06-26 09:05:26 · 171 阅读 · 0 评论 -
Generate Parentheses(C++)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.class Solution {public: vector<string> generateParenthesis(int n) { vector<...原创 2018-05-23 13:23:11 · 367 阅读 · 0 评论 -
Median of Two Sorted Arrays(C++)
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m=nums1.size(),n=nums2.size(); if((m+n)%2==1) re...原创 2018-05-15 17:20:10 · 475 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).Note:-100.0 < x < 100.0n is a 32-bit signed integer, within the range [−231, 231 − 1]class Solution {public: double myPow(do...原创 2018-05-27 09:07:43 · 971 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off as "t...原创 2018-05-26 11:30:22 · 189 阅读 · 0 评论 -
Sum Root to Leaf Numbers
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 number 123.Find the tota...原创 2018-06-05 08:07:26 · 151 阅读 · 0 评论 -
Invert Binary Tree
Invert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1/** * Definition for a binary tree node. * struct TreeNode { *...原创 2018-06-06 08:14:15 · 121 阅读 · 0 评论 -
树上最远距离
从二叉树的节点A出发,可以向上或者向下走,但沿途的节点只能经过一次,当到达节点B时,路径上的节点数叫作A到B的距离。对于给定的一棵二叉树,求整棵树上节点间的最大距离。给定一个二叉树的头结点root,请返回最大距离。保证点数大于等于2小于等于500./*struct TreeNode { int val; struct TreeNode *left; struct TreeNode...原创 2018-06-13 01:50:20 · 230 阅读 · 0 评论 -
方格移动
在XxY的方格中,以左上角格子为起点,右下角格子为终点,每次只能向下走或者向右走,请问一共有多少种不同的走法给定两个正整数int x,int y,请返回走法数目。保证x+y小于等于12。测试样例:2,2返回:2class Robot {public: int Cmn(int m,int n) { if(n==0) return 1; i...原创 2018-06-15 08:49:46 · 512 阅读 · 0 评论 -
孤傲的A
A(A也是他的编号)是一个孤傲的人,在一个n个人(其中编号依次为1到n)的队列中,他于其中的标号为b和标号c的人都有矛盾,所以他不会和他们站在相邻的位置。现在问你满足A的要求的对列有多少种?给定人数n和三个人的标号A,b和c,请返回所求答案,保证人数小于等于11且大于等于3。测试样例:6,1,2,3288class LonelyA {public: int getNum(int n) ...原创 2018-06-15 08:50:12 · 358 阅读 · 0 评论 -
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.Each solu...原创 2018-06-16 00:32:57 · 242 阅读 · 0 评论 -
N-Queens II
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 the number of distinct solutions to the n-queens puzzle...原创 2018-06-16 00:32:42 · 275 阅读 · 0 评论 -
合法的括号序列匹配数
假设有n对左右括号,请求出合法的排列有多少个?合法是指每一个括号都可以找到与之配对的括号,比如n=1时,()是合法的,但是)(为不合法。给定一个整数n,请返回所求的合法排列数。保证结果在int范围内。测试样例:1返回:1class Parenthesis {public: int Cmn(int m,int n) { if(m==n||n==0) r...原创 2018-06-16 00:33:47 · 3415 阅读 · 0 评论 -
错装信封
有n个信封,包含n封信,现在把信拿出来,再装回去,要求每封信不能装回它原来的信封,问有多少种装法?给定一个整数n,请返回装发个数,为了防止溢出,请返回结果Mod 1000000007的值。保证n的大小小于等于300。测试样例:2返回:1class CombineByMistake {public: int countWays(int n) { // write code her...原创 2018-06-17 01:39:23 · 795 阅读 · 0 评论 -
Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST's: 1 3 ...原创 2018-06-24 00:16:40 · 145 阅读 · 0 评论 -
Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:In...原创 2018-06-22 00:23:16 · 288 阅读 · 0 评论 -
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the ...原创 2018-06-20 01:20:24 · 217 阅读 · 0 评论 -
Regular Expression Matching(递归)
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element....原创 2018-08-12 10:53:23 · 202 阅读 · 0 评论