摘要:定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素。 要求函数 min、push 以及 pop 的时间复杂度都是 O(1)。 >test.exeStack is not initialized.stack push 9stack push 5stack push 6stack pus
阅读全文
随笔分类 - 算法
摘要:定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素。 要求函数 min、push 以及 pop 的时间复杂度都是 O(1)。 >test.exeStack is not initialized.stack push 9stack push 5stack push 6stack pus
阅读全文
摘要:## 1. 把二元查找树转变成排序的双向链表 ## ### 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 ### 要求不能创建任何新的结点,只调整指针的指向。 10 / \ 6 14 / \ / \ 4 8 12 16 转换成双向链表 4=6=8=10=12=14=16。 首
阅读全文
摘要:从root开始遍历,如果n1和n2中的任一个和root匹配,那么root就是LCA。 如果都不匹配,则分别递归左、右子树,如果有一个 key(n1或n2)出现在左子树,并且另一个key(n1或n2)出现在右子树,则root就是LCA. 如果两个key都出现在左子树,则说明LCA在左子树中,否则在右子...
阅读全文
摘要:解决二叉树的很多问题的方案都是基于对二叉树的遍历。遍历二叉树的前序,中序,后序三大方法算是计算机科班学生必写代码了。其递归遍历是人人都能信手拈来,可是在手生时写出非递归遍历恐非易事。正因为并非易事,所以网上出现无数的介绍二叉树非递归遍历方法的文章。可是大家需要的真是那些非递归遍历代码和讲述吗?代码早...
阅读全文
摘要:#include "List.h"#include #include using namespace std;#define max(a, b) (a) > (b) ? (a) : (b)// LeetCode, Longest Substring Without Repeating Charact...
阅读全文
摘要:#ifndef List_h__#define List_h__#include struct ListNode{ int value; ListNode* pNext; ListNode(int n) :value(n), pNext(nullptr){}};class List...
阅读全文
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, retu...
阅读全文
摘要:#include "stdafx.h"void PrintFunc(int a[], int n){ for (int i = 0; i low; --j) { if (a[j]AlgoTest.exe0 8 7 6 5 4 3 2 1 90 1 7 6 5...
阅读全文
摘要:#include "stdafx.h"void PrintFunc(int a[], int n){ for (int i = 0; i =0&& a[j]>x)//重点 { a[j + 1] = a[j]; j...
阅读全文
摘要:VC++2010下编译STLport,Boost最近在想向Boost转移,努力掌握Boost代码的过程中,STLport版本:5.2.1Boost版本:1.4.6.1 (1.4.7.0也OK)编译器Visual Studio 2010STLPort的编译,选择Microsoft Visual Studio 20010下面的Visual Studio Tools下面的命令行环境编译工具,Visual Studio Command Prompt(2010)。进入STLport目录,运行1234cd E:\HaveFun\STLport\STLport.5.2.1configure.bat msvc
阅读全文
摘要:中缀表达式的计算主要要转换为后缀表达式。例如 中缀表达式->(1+2)*3-4 转换为后缀表达式 12+3*4-至于后缀表达式的计算就很容易了 设定一个栈,然后后缀表达式从左到右一次进栈,如果当前的入栈的是数字就将其压入栈中,如果是运算符,就从栈中弹出两个数字进行相应的运算,然后将运算后的数字压回栈中。当字符串完全处理之后,栈顶就是运算结果PS:输入的后缀表达式是合法的才行。那么中缀表达式如何转换为后缀表达式?(ch接受中缀表达式传递过来的字符)1:ch 是'(' 放入栈;2: ch 是 ‘)’一次输入栈中的运算符,直到遇到‘(’为止3:如果ch是其它的合法字符,将ch与
阅读全文
|