在我们学习了栈和队列之后,今天来通过几道练习题来巩固一下我们的知识。
题目链接:232. 用栈实现队列 - 力扣(Leetcode)
这道题难度不是很大,重要的是我们对结构认识的考察,由于这篇文章我们是通过C语言解决的,所以我们必须先去构造一个栈,并且可以进行栈的各种操作,最终实现队列的实现。
typedef int datetype;
typedef struct Stack
{
datetype* a;
int capacity;
int top;
}stack;
//初始化
void stackInit(stack* p);
//销毁
void stackDestroy(stack* p);
//入栈
void stackPush(stack* p, datetype x);
//出栈
void stackPop(stack* p);
//取栈顶数据
datetype stackTop(stack* p);
//数据个数
int stackSize(stack* p);
//判断是否为空
bool stackEmpty(stack* p);
bool isValid(char* s);
void stackInit(stack* p)
{
assert(p);
p->a = NULL;
p->capacity = 0;
p->top = 0;
}
void stackPush(stack* p,datetype x)
{
assert(p);
if (p->capacity == p->top)
{
int newCapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
datetype* tmp = (datetype*)realloc(p->a, newCapacity * sizeof(datetype));
if (tmp == NULL)
{
perror("realloc");
exit(-1);
}
p->a = tmp;
p->capacity=newCapacity;
}
p->a[p->top] = x;
p->top++;
}
void stackPop(stack* p)
{
assert(p);
assert(!stackEmpty(p));
p->top--;
}
void stackDestroy(stack* p)
{
assert(p);
if (p->capacity == 0)
return;
free(p->a);
p->a = NULL;
p->capacity = 0;
p->top = 0;
}
datetype stackTop(stack* p)
{
assert(p);
assert(!stackEmpty(p));
return p->a[p->top-1];
}
bool stackEmpty(stack* p)
{
assert(p);
return p->top==0;
}
int stackSize(stack* p)
{
assert(p);
return p->top;
}
//定义进行出数据和入数据的栈
typedef struct {
stack pushST;
stack popST;
} MyQueue;
//创建队列,使用两个栈来实现
MyQueue* myQueueCreate() {
MyQueue* tmp=(MyQueue*)malloc(sizeof(MyQueue));
stackInit(&tmp->pushST);
stackInit(&tmp->popST);
return tmp;
}
//直接将数据入pushST
void myQueuePush(MyQueue* obj, int x) {
stackPush(&obj->pushST,x);
}
//如过popST内不为空,直接输出数据,如果为空,将pushST数据都入到popST内
int myQueuePop(MyQueue* obj) {
if(stackEmpty(&obj->popST))
{
while(!stackEmpty(&obj->pushST))
{
stackPush(&obj->popST,stackTop(&obj->pushST));
stackPop(&obj->pushST);
}
}
int front=stackTop(&obj->popST);
stackPop(&obj->popST);
return front;
}
int myQueuePeek(MyQueue* obj) {
if(stackEmpty(&obj->popST))
{
while(!stackEmpty(&obj->pushST))
{
stackPush(&obj->popST,stackTop(&obj->pushST));
stackPop(&obj->pushST);
}
}
return stackTop(&obj->popST);
}
bool myQueueEmpty(MyQueue* obj) {
return stackEmpty(&obj->pushST)&&stackEmpty(&obj->popST);
}
void myQueueFree(MyQueue* obj) {
stackDestroy(&obj->pushST);
stackDestroy(&obj->popST);
free(obj);
obj=NULL;
}
这道题我们使用两个栈,一个栈负责出数据,一个栈负责入数据,当入数据时,直接push即可,当出数据时,我们使用两个栈配合,使用栈先入先出的特点,将pushST的内容入到popST时,数据就已经变成了逆序,所以在popST栈内,直接出就可以实现队列的先入后出的功能。
题目链接:225. 用队列实现栈 - 力扣(Leetcode)
这道题与上道题类似,都是对结构的考察,使用队列先入先出的特点,通过两个队列之前出数据入数据后,可以实现栈的功能,当一个队列出队进入另外一个队列,只剩一个数据时,直接将这个数据出队列,两个队列互相配合,最终将所有数出队,实现栈的功能。
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
// size_t _size;
}Queue;
//void QueueInit(QueueNode** pphead, QueueNode** pptail);
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
while (cur != NULL)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
//if (pq->head == NULL)
// return;
assert(!QueueEmpty(pq));
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
if (pq->head == NULL)
{
pq->tail = NULL;
}
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
int QueueSize(Queue* pq)
{
assert(pq);
int n = 0;
QueueNode* cur = pq->head;
while (cur)
{
++n;
cur = cur->next;
}
return n;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* ret =(MyStack*)malloc(sizeof(MyStack));
QueueInit(&ret->q1);
QueueInit(&ret->q2);
return ret;
}
//哪个队列不为空,就在哪个队列入数据
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
//找到为空和不为空的队列,将不为空的队列出为只剩一个数据,直接出这个数据
int myStackPop(MyStack* obj) {
Queue* empty = &obj->q1;
Queue* nonempty = &obj->q2;
if(!QueueEmpty(&obj->q1))
{
empty = &obj->q2;
nonempty = &obj->q1;
}
while(QueueSize(nonempty)>1)
{
QueuePush(empty,QueueFront(nonempty));
QueuePop(nonempty);
}
int ret=QueueFront(nonempty);
QueuePop(nonempty);
return ret;
}
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
题目链接:622. 设计循环队列 - 力扣(Leetcode)
我们之前实现队列是使用链表来实现的,当然也可以使用顺序表来实现,考虑到这道题循环队列的特殊结构,我们使用顺序表和链表两种方式来解决这个问题。
首先定义每个结点的结构,再定义一个结构来存head,tail,数据个数size,最大容量capacity,当数据个数等于最大容量时,说明队列已满,当数据个数size为0时,说明队列为空。
typedef struct qNode
{
int data;
struct qNode* next;
}qNode;
typedef struct {
qNode* head;
qNode* tail;
int size;
int capacity;
} MyCircularQueue;
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* cq=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
cq->head=cq->tail=NULL;
cq->capacity=k;
cq->size=0;
return cq;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
qNode* newNode=(qNode*)malloc(sizeof(qNode));
newNode->data=value;
newNode->next=NULL;
if(obj->size==0)
{
obj->head=obj->tail=newNode;
}
else
{
obj->tail->next=newNode;
obj->tail=newNode;
}
obj->size++;
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
qNode* next=obj->head->next;
free(obj->head);
obj->head=next;
obj->size--;
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->head->data;
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->tail->data;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return !obj->size;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return obj->size==obj->capacity;
}
void myCircularQueueFree(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
free(obj);
return;
}
qNode* cur=obj->head;
while(cur!=obj->tail)
{
qNode* next = cur->next;
free(cur);
cur = next;
}
free(obj);
obj=NULL;
}
使用顺序表,当head==tail时,说明队列为空,当head==tail+1时,说明队列为满。
typedef struct {
int* a;
int head;
int tail;
int capacity;
} MyCircularQueue;
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* cq=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
cq->a = (int*)malloc(sizeof(int)*(k+1));
cq->head=cq->tail=0;
cq->capacity=k;
return cq;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->head==obj->tail;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->tail+1)%(obj->capacity+1)==obj->head;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->tail++]=value;
obj->tail = obj->tail % (obj->capacity+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
obj->head++;
obj->head=obj->head%(obj->capacity+1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->a[obj->head];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->a[obj->tail];
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
free(obj);
}
我们今天讲解了栈与队列的笔试题,希望可以得到大家的支持。
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
我有一个涉及多台机器、消息队列和事务的问题。因此,例如用户点击网页,点击将消息发送到另一台机器,该机器将付款添加到用户的帐户。每秒可能有数千次点击。事务的所有方面都应该是容错的。我以前从未遇到过这样的事情,但一些阅读表明这是一个众所周知的问题。所以我的问题。我假设安全的方法是使用两阶段提交,但协议(protocol)是阻塞的,所以我不会获得所需的性能,我是否正确?我通常写Ruby,但似乎Redis之类的数据库和Rescue、RabbitMQ等消息队列系统对我的帮助不大——即使我实现某种两阶段提交,如果Redis崩溃,数据也会丢失,因为它本质上只是内存。所有这些让我开始关注erlang和
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD
本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01 客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02 数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试在Rails上安装ruby,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf