
算法合集
文章平均质量分 68
水晶莲
这个作者很懒,什么都没留下…
展开
-
创建二叉排序树C语言实现
#include“stdio.h”int main(){ printf("dsafdasfd");}转载 2014-06-23 17:04:42 · 2999 阅读 · 0 评论 -
ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S转载 2014-08-11 09:46:29 · 322 阅读 · 0 评论 -
经典DP问题
经典动态规划问题解析:转载 2014-08-08 19:19:45 · 389 阅读 · 0 评论 -
最短路径(动态规划)
#include#include#include "stack"#include "iostream"using namespace std;#define x 9999#define max 9999int dist[10];//记录最短路径为多少int path[10];//记录最短路径void fpath(int a[][10]);int froute(int a[]原创 2014-08-09 21:00:15 · 1603 阅读 · 0 评论 -
反转链表递归和非递归实现(面试题 16)
反转链表的递归实现和非递归实现原创 2014-06-30 00:07:54 · 497 阅读 · 0 评论 -
求1+2+…+n
题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。转载 2014-09-09 09:32:43 · 423 阅读 · 0 评论 -
1的个数
LONGLONG Sum1s(ULONGLONG n){ ULONGLONG iCount =0; ULONGLONG iFactor =1; ULONGLONG iLowerNum =0; ULONGLONG iCurrNum =0; ULONGLONG iHigherNum =0; while (n/iFactor !=0) { iLowerNum =n -(n/iFac转载 2014-09-11 09:14:24 · 371 阅读 · 0 评论 -
栈的push、pop 序列
栈的push、pop 序列 题目:输入两个整数序列。其中一个序列表示栈的push 顺序, 判断另一个序列有没有可能是对应的pop 顺序。 为了简单起见,我们假设push 序列的任意两个整数都是不相等的。 比如输入的push 序列是1、2、3、4、5,那么4、5、3、2、1 就有可能是一个pop 系列。原创 2014-09-10 23:52:19 · 922 阅读 · 0 评论 -
最长递减子序列
#include using namespace std; // 遍历最长递减子序列,递归法 // A - 源序列数组 // B - 通过DP求出的辅助数组 // k - 使得B[i]最大的i值 void max_dec_subseq_traverse(int* A, int* B, int k) { int i; for (i = k; i >= 0;转载 2014-09-11 13:54:47 · 543 阅读 · 0 评论 -
n对括号匹配的种类
#include using namespace std;//匹配数int num=0;//判断当前n对括号是否匹配bool isMatch(int n,char* bracket){ int left_num=0,right_num=0; for(int i=0;i<2*n;++i) { if(bracket[i]=='l') left_num++; else转载 2014-09-11 22:20:46 · 600 阅读 · 0 评论 -
寻找二叉树两个结点的最低共同父节点
寻找二叉树两个结点的最低共同父节点题目:二叉树的结点的定义如下:struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。转载 2014-09-12 10:35:33 · 846 阅读 · 0 评论 -
基本排序之冒泡排序
#include"iostream"using namespace std;void Swap(int& a1,int& a2){ int t; t =a1; a1 =a2; a2 =t;}void bubblesort(int A[],int n){ for(int i=0;i<n;i++) for(int j=0;j<n-i;j++) { if(A[j]原创 2014-09-01 09:06:05 · 324 阅读 · 0 评论 -
括号匹配
#include "iostream"#include "stack"using namespace std;bool pipei(char* str){ stack st; char* p=str; if (!str) { return false; } int index =0; while(*p) { switch(*p) { case '{':原创 2014-08-08 14:04:54 · 381 阅读 · 0 评论 -
0-1背包问题
#include using namespace std;int length;void PrintSolution(int *flag){ for (int i=0;i<length;i++) { if (flag[i]==1) { cout<<i+1<<" "; } } cout转载 2014-08-08 13:22:08 · 317 阅读 · 0 评论 -
分数化小数
//整数相除.cpp#include#includeusing namespace std;const int maxn = 100; //设置字符串的最大位数const int max_INT = 10000; int reminder_exist[max_INT];int reminder_pos[max_INT];void div(const int a, const in转载 2014-08-08 15:52:10 · 622 阅读 · 2 评论 -
二叉排序树的创建,以及前序、中序、后序遍历的递归实现
#include "stdlib.h"#include "stdio.h"typedef int KeyType;typedef struct Node { KeyType key; struct Node* left; struct Node* right; struct Node* parent;}Node,*pNode;void insert(pNode* root,原创 2014-06-29 13:01:48 · 585 阅读 · 0 评论 -
二叉树的三种遍历的非递归实现
1.先序遍历 根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下: 对于任一结点P: 1)访问结点P,并将结点P入栈; 2)判断结点P的左孩子是否为空,若为空转载 2014-06-29 13:38:15 · 442 阅读 · 0 评论 -
重建二叉树(面试题 6)
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树转载 2014-06-29 15:05:09 · 363 阅读 · 0 评论 -
二叉树的镜像 (面试题 19)
题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。原创 2014-06-29 15:22:20 · 413 阅读 · 0 评论 -
合并两个有序链表(面试题 17)
题目:输入两个递增de原创 2014-06-30 14:21:05 · 458 阅读 · 0 评论 -
计算二叉树的高度的递归和非递归实现
1.递归方法int FindTreeDeep(BinTree BT){ int deep=0; if(BT){ int lchilddeep=FindTreeDeep(BT->lchild); int rchilddeep=FindTreeDeep(BT->rchild); deep=lchilddeep>=rchi转载 2014-06-29 15:38:44 · 5036 阅读 · 1 评论 -
二叉搜索树转双向链表(面试题27)
BSTreeNode *ConvertNode(BSTreeNode *pNode,bool asRight) { if(!pNode) { return NULL; } BSTreeNode *pLeft = NULL; BSTreeNode *pRight = NULL; //如果左孩子不空,则递归转载 2014-06-29 21:02:21 · 395 阅读 · 0 评论 -
数字在排列数组中出现的次数(面试题 38)
题目:统计一个数字在排列中出现的次数。例如shurujpa原创 2014-07-01 09:37:09 · 351 阅读 · 0 评论 -
字符串的排列和组合实现(面试题 28)
1.字符串的排列(递归实现)原创 2014-07-01 09:27:00 · 498 阅读 · 0 评论 -
约瑟夫环问题
描述: 现在有n个竞争者围坐一圈,争夺一个很有吸引力的工作(年薪100w $)。假设这些人编号1,2,。。。,n。第一次从1开始报数,数到m(m>0)的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列。以此类推,直到所有人出列为止。老板说最后一个出列的人将获得这份工作。 如果你也想竞争这份工作,那么你会坐着哪个位置上? 实现一个函数,当老板告诉你n和m时,返回得到工作的那原创 2014-07-29 15:51:12 · 569 阅读 · 0 评论 -
算术表达式求值
第三题、条件表达式求值 描述: 给定一个以字符串形式表示的算术表达式,计算该表达式的值。表达式支持如下运算:“+、-、*、/”,其中“*”和“/”的优先级要高于“+”和“-”;不需要考虑括号,且表达式之间没有空格;例如:对于表达式"3-2+15*2",该表达式值为31. 运行时间限制: 60 Sec 内存限制: 256 MByte 输入: 加减乘除四则运算表达式,长原创 2014-07-29 18:05:32 · 760 阅读 · 0 评论 -
不用加减乘除运算符求解两个数加减乘除
1.计算 a+b原创 2014-09-22 20:59:49 · 820 阅读 · 0 评论