链表
合并链表
递归删除值为X的结点(不带头结点)
递归从尾到头输出链表L中的值
删除单链表最小值结点
链表原地转置
使带头结点单链表递增有序
两个单链表的第一个公共结点
将两个循环单链表 ,链表头指针分别为h1,h2,将链表h2链接到链表h1之后,保证链接后链表仍然保持循环链表形式
栈和队列
共享栈
括号匹配
栈模拟队列
队列模拟栈
方法一
class MyStack {
public:
queue<int> queue1;
queue<int> queue2;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
queue2.push(x);
while (!queue1.empty()) {
queue2.push(queue1.front());
queue1.pop();
}
swap(queue1, queue2);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int r = queue1.front();
queue1.pop();
return r;
}
/** Get the top element. */
int top() {
int r = queue1.front();
return r;
}
/** Returns whether the stack is empty. */
bool empty() {
return queue1.empty();
}
};
方法二
class MyStack {
public:
queue<int> q;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
int n = q.size();
q.push(x);
for (int i = 0; i < n; i++) {
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int r = q.front();
q.pop();
return r;
}
/** Get the top element. */
int top() {
int r = q.front();
return r;
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
};
后缀表达式求值(选看*)
输入:tokens = [“4”,“13”,“5”,"/","+"]
输出:6
解释:该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6
class Solution {
public:
int calculate(int a,int b,string op){
int res;
switch(op[0]){
case '+':res= a+b;break;
case '-':res= a-b;break;
case '*':res= a*b;break;
case '/':res= a/b;break;
default:break;
}
return res;
}
bool isNum(string str)
{
if((str[0] == '*' || str[0] == '/' || str[0] == '+' || str[0] == '-')
&& str.size() == 1)
{
return false;
}
return true;
}
int evalRPN(vector<string>& tokens) {
stack<int> s;
int a,b;
for(int i=0;i<tokens.size();i++){
if(isNum(tokens[i]))
{
s.push(atoi(tokens[i].c_str()));
}
else{
b = s.top();
s.pop();
a = s.top();
s.pop();
s.push(calculate(a,b,tokens[i]));
}
}
return s.top();
}
};
串
Brute-Force
KMP
Next
Nextval
树
先序遍历
递归
非递归
中序遍历
递归
非递归
后序遍历
递归
非递归
层序遍历
树的高度
层序遍历求树高
递归求树高
int Depth(BiTree T){
if(T==NULL)return 0;
else{
m=depth(T->lchild);
n=depth(T->lchild);
}
if(m>n)return (m+1);
else return(n+1);
}
从下到上,从右到左遍历树
判断完全二叉树
递归交换左右子树
计算双分支结点数
先序遍历第k个结点的值
将二叉树叶节点从左到右顺序连成一个链表
相似二叉树
计算WPL
先序遍历
二叉树转表达式
判断二叉排序树
判断结点在排序二叉树中层次
判断平衡二叉树
输出二叉排序树中最大最小值
从大到小输出二叉排序树的值
图
DFS
BFS
单源最短路径
查找
顺序查找
折半查找
排序
直接插入排序
折半插入排序
希尔排序
冒泡排序
快速排序
简单选择排序
堆排序
归并排序