![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 78
fangjian1204
这个作者很懒,什么都没留下…
展开
-
leetcode 之 Scramble String
leetcode 之 Scramble String原创 2014-09-07 09:08:29 · 1267 阅读 · 0 评论 -
阿里面试题之求完全二叉树的最后一层的最后一个节点
完全二叉树的最后一层的最后一个节点原创 2014-09-10 13:32:07 · 9129 阅读 · 1 评论 -
leetcode 之 Symmetric Tree 镜像树
Symmetric Tree原创 2014-08-26 10:55:10 · 1996 阅读 · 0 评论 -
leetcode 之 Validate Binary Search Tree
Validate Binary Search Tree原创 2014-08-26 10:09:18 · 923 阅读 · 0 评论 -
排序算法总结
1、冒泡排序2、插入排序3、快排原创 2014-09-08 21:41:03 · 830 阅读 · 0 评论 -
leetcode之Divide Two Integers
Divide Two IntegersDivide two integers without using multiplication, division and mod operator.分析:原创 2014-09-08 21:30:43 · 1155 阅读 · 0 评论 -
常用库函数的编写
1、strcpy2、strcmpint strcmp(const char* str1,const char* str2){ assert(str1!=NULL && str2!=NULL); while(*str1 && *str2 && *str1 == *str2) { str1++; str2++; } int res = *str1 - *str2; if(原创 2014-09-08 20:38:30 · 1077 阅读 · 0 评论 -
素数、亲和数、完数的求法
这几个问题都可以用一个辅助数组来帮助求解,也有的称为是伴随数组。1、素数筛选法求素数:把从1开始的、某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉。剩下的数中选择最小的数是素数,然后去掉它的倍数。依次类推,直到筛子为空时结束。void prime(int n){ vector isPrime(n+1,true); int i; for (i = 2;i*i <=原创 2014-09-08 19:49:03 · 1474 阅读 · 0 评论 -
leetcode 之 Median of Two Sorted Arrays
Median of Two Sorted Arrays原创 2014-09-08 11:32:06 · 1156 阅读 · 0 评论 -
大整数的乘法运算
思想:每次取第二个数的最高位进行一次乘法,把结果乘以10和下一次结果相加,题目来源:leetcodeclass Solution {public: //一个整数乘以一个个位数 string multOneBit(string num,int data) { int i = num.size() - 1,carry = 0; string res;原创 2014-08-11 18:52:06 · 1061 阅读 · 0 评论 -
leetcode 之 Jump Game
Jump Game原创 2014-09-08 10:23:30 · 1206 阅读 · 0 评论 -
leetcode 之 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie,原创 2014-09-08 08:58:22 · 1189 阅读 · 0 评论 -
编程之美之二分查找总结
二分查找原创 2014-08-20 15:47:16 · 2939 阅读 · 0 评论 -
leetcode 之 Triangle
Triangle原创 2014-09-08 15:09:26 · 1012 阅读 · 0 评论 -
Valid Sudoku 和 Sudoku Solver
题目一:Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.原创 2014-08-12 15:13:50 · 735 阅读 · 0 评论 -
leetcode 之 Rotate Image
Rotate Image原创 2014-09-10 11:03:02 · 1122 阅读 · 0 评论 -
归并排序的应用
一、归并排序算法思路就是把数组分成左右两个部分,然后再进行归并两个有序表void merge(int* num,int start,int mid,int end,int* copy){ int i = start,m = mid,j = mid+1,n = end,k=start; while(i <= m && j <= n) { if(num[i] < num[j])co原创 2014-08-27 11:20:09 · 1001 阅读 · 0 评论 -
leetcode之Word Break
Word BreakGiven 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, givens = "leetcode",原创 2014-09-06 17:31:38 · 1103 阅读 · 0 评论 -
母函数的应用
参考文献在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供关于这个序列的信息。使用母函数解决问题的方法称为母函数方法。母函数可分为很多种,包括普通母函数、指数母函数、L级数、贝尔级数和狄利克雷级数。对每个序列都可以写出以上每个类型的一个母函数。构造母函数的目的一般是为了解决某个特定的问题,因此选用何种母函数视乎原创 2014-09-15 15:55:10 · 1095 阅读 · 0 评论 -
编程之美leetcode之编辑距离
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operati原创 2014-08-18 19:17:18 · 1432 阅读 · 0 评论 -
编程之美之电话号码对应英语单词
题目一:根据电话上字母和数字的对应关系,用一个有意义的单词来表述一个电话号码,如用computer表示26678837题目二:反过来,给定一个电话号码,是否可以用一个单词来表示呢?怎样表示最快呢?显然不是所有的电话号码都可以对应到单词上去首先来看看leetcode上一个类似的题目:Letter Combinations of a Phone Number G原创 2014-08-15 14:34:20 · 1622 阅读 · 0 评论 -
待字闺中之四个数的和
盒子中有n张卡片,上面的数字分别为k1,k2,...,kn。你有4次机会,每抽一次,记录下卡片上的数字,再将卡片放回盒子中。如果4个数字的和等于m。则你就赢得游戏,否则就是输。直觉上,赢的可能性太低了。请你给出程序,判断是否有赢的可能性。尽量提高方法的效率。分析:这个题目,和之前Google的一个概率的题目是类似的,当然解决的方法也是类似的,思路大家可以找找前面的题目,不再赘述。这个题目其原创 2014-08-08 14:20:43 · 1018 阅读 · 0 评论 -
leetcode 之 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O X原创 2014-08-28 13:26:50 · 934 阅读 · 0 评论 -
子序列与子串问题总结
1、最长递增子序列扩展:双duan原创 2014-08-21 15:18:36 · 1872 阅读 · 0 评论 -
编程之美之求二叉树中节点的最大距离
题目:如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。原创 2014-08-20 18:49:00 · 1251 阅读 · 0 评论 -
leetcode 之 Permutation Sequence
Permutation SequenceThe 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):原创 2014-08-28 10:40:14 · 1044 阅读 · 0 评论 -
leetcode之Search in Rotated Sorted Array,剑指offer之旋转数组的最小数字
输入一个递增数组的旋转,原创 2014-08-27 15:52:49 · 1312 阅读 · 0 评论 -
leetcode 之 Unique Paths
Unique PathsA robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The r原创 2014-08-27 16:20:35 · 1018 阅读 · 0 评论 -
leetcode之Median of Two Sorted Arrays
Median of Two Sorted ArraysThere 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 (原创 2014-08-27 14:04:40 · 1085 阅读 · 0 评论 -
leetcode之Gray Code
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the cod原创 2014-08-27 14:20:34 · 1073 阅读 · 0 评论 -
leetcode之n皇后问题
leetcode上有两个关于n皇后的问题,两个题目基本是一样的,只是第二个是把所有的排法求出来。n皇后最简单的就是用递归,每次判断一行的一个位置,如果合法,就判断下一行,不合法再判断下一个位置N-Queens II Follow up for N-Queens problem.Now, instead outputting board configuration原创 2014-08-11 14:13:14 · 1980 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
题目一:Populating Next Right Pointers in Each NodeGiven a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Popul原创 2014-08-12 08:50:53 · 905 阅读 · 0 评论 -
待字闺中之最长等差数列分析
原题给定未排序的数组,请给出方法找到最长的等差数列。分析题目描述比较简单,但是有一个问题我们需要首先搞清楚:等差数列中的数字,是否要和原始数组中的顺序一致。题目中,并没有说明,这个就需要大家在面试的过程中和面试官进行交流。我们在这里对两种情况都进行讨论保证数字的顺序等差数列是要求相邻两个元素之间的差是相同的。那我们可以记录下来数组中任意两个数的差,并且记录下原创 2014-09-06 08:44:53 · 1268 阅读 · 0 评论 -
leetcode 之 Distinct Subsequences
Distinct SubsequencesGiven a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string b原创 2014-09-06 21:20:26 · 1139 阅读 · 0 评论 -
待字闺中之删除字符分析
删除字符串中的“b”和“ac”,需要满足如下的条件:1. 字符串只能遍历一次2. 不能够使用额外的空间例如:1. acbac ==> ""2. aaac ==> aa3. ababac ==> aa4. bbbbd ==> d进一步思考:如何处理aaccac呢,需要做哪些改变呢?分析首先要明白从字符串中删除某些字符该如原创 2014-09-06 10:31:16 · 1351 阅读 · 0 评论 -
leetcode之Combination Sum
Combination SumGiven 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原创 2014-08-22 15:17:27 · 994 阅读 · 0 评论 -
leetcode之Partition List,Reverse Nodes in k-Group
Partition ListGiven a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of原创 2014-08-21 18:46:19 · 1130 阅读 · 0 评论 -
leetcode之通配符
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequen原创 2014-08-18 17:55:57 · 1322 阅读 · 0 评论 -
求两个数组的交集
问题: 给你两个排序的数组,求两个数组的交集。比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5.思路:1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存。该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度。2. 因为A B 都排过序,所以,每一次从B数组取值后,可以利用二分查找看是否在数转载 2014-09-18 19:27:55 · 866 阅读 · 0 评论 -
最近公共祖先
参考文献1参考文献2题目:输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点变种一:二叉树是二分查找树,如果根节点比两个节点都小,则访问右子树,都大则访问左子树,否则即为结果变种二:每个节点有指向父节点的指针,则转变为查找两个单链表的第一个公共节点变种三:对于普通二叉树,下面给出两种方法方法一:二叉树的后序遍历,先看节点是否在左子树,再看右子树,然后根据左右子原创 2014-08-21 15:02:53 · 1001 阅读 · 0 评论