
算法
面试高频
tromaker3
这个作者很懒,什么都没留下…
展开
-
String的实现
#include<iostream>using namespace std;class String { /*String()//无参构造 { _str=new char[1]; *_str='\0'; } String(const char *str)//有参构造 { _str=new char[strlen(str)+1]; strcpy(_str,...原创 2020-04-26 15:50:45 · 194 阅读 · 0 评论 -
双链表的反转
//双向链表的反转struct List { int value; struct List *last,*next;};//反转双向List *reverse(List *head){ List *cur=head,*pre=NULL; while (cur!=NULL) { List *nxt=cur->next;//首先保存cur的下一节点到nxt,否则会失去下...原创 2020-04-22 18:34:59 · 334 阅读 · 0 评论 -
两个有序链表的合并
已知输入的两个链表递增有序,要使输出的链表依然递增有序//合并有序链表list *mergelists(list *p1,list *p2){ if (p1==NULL) { return p2; } if (p2==NULL) { return p1; } if (p1->value < p2->value) { p1->next=me...原创 2020-04-22 11:28:46 · 155 阅读 · 0 评论 -
写一个单例
#include using namespace std;class Singleton{public:static Singleton& Instance(){static Singleton theSingleton;return theSingleton;}void doSomeThong();private:Singleton();~Singleton()...原创 2020-04-16 19:03:49 · 159 阅读 · 0 评论 -
递归求数组的最大值
int arraymax(int a[],int n){ if (n==1) { return a[0]; } else { return a[n-1] > arraymax(a,n-1) ? a[n-1]:arraymax(a,n-1); }}原创 2020-04-16 18:53:37 · 413 阅读 · 0 评论 -
反转链表
#include <iostream>using namespace std;typedef struct List { int value; struct List *next;}list,*ListNode;//反转list *reverse(ListNode head){ //终止条件找到链表的最后一个节点 if (head==NULL || head-...原创 2020-04-16 15:06:52 · 121 阅读 · 0 评论 -
二叉树的前中后遍历(递归与非递归)、BST插入、删除、创建、查找
#include <iostream>#include <stack>using namespace std;#define len 15typedef int ElemType;typedef struct BiTNode { ElemType data; struct BiTNode *lchild,*rchild;}BiTNode ,*BiTre...原创 2020-03-23 15:22:26 · 208 阅读 · 0 评论 -
堆排序C++
#include<iostream>using namespace std;//headify初始化堆,堆化//a为待排序的数列,i为当前节点(非叶子节点),n为待排序数列的长度void heapify(int a[],int i,int n){ int l = 2 * i + 1;//当前节点i的左孩子 int r = 2 * i + 2;//当前节点i的右孩子 i...原创 2020-04-07 15:23:47 · 116 阅读 · 0 评论 -
统计二叉树的叶子(非叶子、度为1、度为2)结点个数
int LeafCount(Bitree T){ int count; if(T==NULL) { return 0; } if(T->lchild==NULL && T->rchild==NULL) { count++; } LeafCount(T->lchild); LeafCou...原创 2020-04-08 09:50:03 · 2482 阅读 · 0 评论 -
判断两颗二叉树是否相同
int issametree(Bitree *t1,Bitree *t2){ if (t1==NULL && t2==NULL)//两个树都为空,则结构相同 { return 1; } else if (t1==NULL || t2==NULL || t1->data!=t2->data)//一个为空一个不为空或者节点的值不相等 { return 0...原创 2020-04-08 15:21:39 · 679 阅读 · 0 评论 -
给定一个整数数组,找出其中两个数相加等于目标值
Example:Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].题目的意思:在无序的数组中找两个数,使得这两个数之和与给定的目标值相等,返回这两个数的下标。大佬们的做法:https://blog.youkuaiyun.com/wz2292667460/article...原创 2020-04-08 16:21:16 · 1068 阅读 · 0 评论 -
最长上升子序列(动态规划)
//最长上升子序列(动态规划解决,时间复杂度O(n²))int LIS(int a[],int n){ //d[i]的值表示前i个数以a[i]结尾的最长的上升子序列的长度 //如果a[i]严格大于[0,i)中的某个数j, //那么说明a[i]就可以在d[j]的基础上形成一个长度为d[j]+1的上升子序列, //得到递推关系:if(a[j]<a[i]) d[i]=max(d[i],...原创 2020-04-13 10:45:18 · 207 阅读 · 0 评论