当前位置: 首页 > news >正文

重庆网站建设注意事项网站推广优化外链

重庆网站建设注意事项,网站推广优化外链,为什么没人做同城购物网站,天津个人做网站栈和队列必备的面试题(第一期) 文章目录栈和队列必备的面试题(第一期)一、题目二、思路(图解)三、存在的问题与隐患(报错提示)(1)s中只有右括号,无…

栈和队列必备的面试题(第一期)


文章目录

  • 栈和队列必备的面试题(第一期)
  • 一、题目
  • 二、思路(图解)
  • 三、存在的问题与隐患(报错提示)
    • (1)s中只有右括号,无左括号
    • (2)返回值处理
    • (3)销毁栈
  • 四、整体源代码
  • 总结


一、题目

在这里插入图片描述


Leedcode链接:https://leetcode.cn/problems/valid-parentheses/


在这里插入图片描述


二、思路(图解)

我们用 来实现这道题,具体如下图!


在这里插入图片描述

这里我们用到 栈 接口实现中的 Stackpush 接口!然后 s++


在这里插入图片描述

这里我们会用到 栈 接口实现中的 StacktopStackpop 接口!下面是 比较方法!


正确的就 s++,知道*s为NULL为止!
在这里插入图片描述


如果不匹配,直接返回 false!
在这里插入图片描述

出栈 + 入栈 + 取栈顶数据 + 比较方法 : 代码如下(示例):

typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;
void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}
void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}
STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}
int StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
bool isValid(char * s)
{ST st;StackInit(&st);while(*s){if(*s == '('|| *s == '['|| *s == '{'){StackPush(&st , *s);s++;}else{STDataType top = StackTop(&st);StackPop(&st);if(*s == ')' && top != '('|| *s == ']' && top != '['|| *s == '}' && top != '{'){StackDestroy(&st);return false;}else{s++;}}}
}

三、存在的问题与隐患(报错提示)

如果现在提交代码,会出现以下的报错 + 问题


(1)s中只有右括号,无左括号

在这里插入图片描述

这里我们应该注意:如果没有左括号直接返回 false 即可!
在这里插入图片描述

代码如下(示例):

while(*s){if(*s == '('|| *s == '['|| *s == '{'){StackPush(&st , *s);s++;}else{//如果没有左括号,栈为空,返回falseif(StackEmpty(&st)){StackDestroy(&st);return false;}STDataType top = StackTop(&st);StackPop(&st);if(*s == ')' && top != '('|| *s == ']' && top != '['|| *s == '}' && top != '{'){StackDestroy(&st);return false;}else{s++;}}}

(2)返回值处理

在这里插入图片描述

如果 栈 不是空,则说明栈中还有左括号未出。即没有匹配,返回是 false

代码如下(示例):

bool ret = StackEmpty(&st);StackDestroy(&st);return ret;

(3)销毁栈

最后不要忘了用 栈 的 StackDestroy 对栈进行销毁否则会导致 内存泄漏等问题

StackDestroy(&st);

四、整体源代码

代码如下(示例):

typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;
void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}
void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}
STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}
int StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
bool isValid(char * s)
{ST st;StackInit(&st);while(*s){if(*s == '('|| *s == '['|| *s == '{'){StackPush(&st , *s);s++;}else{//如果没有左括号,栈为空,返回falseif(StackEmpty(&st)){StackDestroy(&st);return false;}STDataType top = StackTop(&st);StackPop(&st);if(*s == ')' && top != '('|| *s == ']' && top != '['|| *s == '}' && top != '{'){StackDestroy(&st);return false;}else{s++;}}}bool ret = StackEmpty(&st);StackDestroy(&st);return ret;}

在这里插入图片描述


总结

以上就是今天要讲的内容,本文介绍了【Leedcode】中栈和队列必备的面试题(第一期)
如果我的博客对你有所帮助记得三连支持一下,感谢大家的支持!
在这里插入图片描述


文章转载自:
http://dinncopaleoclimate.bkqw.cn
http://dinncoproestrus.bkqw.cn
http://dinncodeintegro.bkqw.cn
http://dinncointerfaith.bkqw.cn
http://dinncosonlike.bkqw.cn
http://dinncoeidograph.bkqw.cn
http://dinncodysphemism.bkqw.cn
http://dinncoderelict.bkqw.cn
http://dinncocharismatic.bkqw.cn
http://dinncoastrut.bkqw.cn
http://dinncosmug.bkqw.cn
http://dinncoheliotype.bkqw.cn
http://dinncotallin.bkqw.cn
http://dinncolurgi.bkqw.cn
http://dinncobobbish.bkqw.cn
http://dinncoindeflectible.bkqw.cn
http://dinncoconsolation.bkqw.cn
http://dinncobetatron.bkqw.cn
http://dinncocuddie.bkqw.cn
http://dinncoaborative.bkqw.cn
http://dinncobroadbrim.bkqw.cn
http://dinncolaminarization.bkqw.cn
http://dinncofrizzy.bkqw.cn
http://dinncostrathclyde.bkqw.cn
http://dinncologomachy.bkqw.cn
http://dinncocentremost.bkqw.cn
http://dinncogreek.bkqw.cn
http://dinncoinveiglement.bkqw.cn
http://dinncodiscretionarily.bkqw.cn
http://dinncorijn.bkqw.cn
http://dinncopsychograph.bkqw.cn
http://dinncosymbolise.bkqw.cn
http://dinncoskateboard.bkqw.cn
http://dinncoluxuriously.bkqw.cn
http://dinncobirthplace.bkqw.cn
http://dinncounbeliever.bkqw.cn
http://dinncooviform.bkqw.cn
http://dinncoset.bkqw.cn
http://dinncocontraterrene.bkqw.cn
http://dinncointerradial.bkqw.cn
http://dinncokneehole.bkqw.cn
http://dinncolongirostral.bkqw.cn
http://dinncodehumanize.bkqw.cn
http://dinncosunshiny.bkqw.cn
http://dinncoworkman.bkqw.cn
http://dinncoglasswork.bkqw.cn
http://dinncolinkwork.bkqw.cn
http://dinncorawness.bkqw.cn
http://dinncomotherliness.bkqw.cn
http://dinnconuclei.bkqw.cn
http://dinncodextropropoxyphene.bkqw.cn
http://dinncospinster.bkqw.cn
http://dinncoprs.bkqw.cn
http://dinncoergophile.bkqw.cn
http://dinncosephardi.bkqw.cn
http://dinncoroselite.bkqw.cn
http://dinncoimino.bkqw.cn
http://dinncoheresiography.bkqw.cn
http://dinncoti.bkqw.cn
http://dinncodelime.bkqw.cn
http://dinncoconcenter.bkqw.cn
http://dinncosplasher.bkqw.cn
http://dinncodisadvantage.bkqw.cn
http://dinncogeomagnetism.bkqw.cn
http://dinncotonguefish.bkqw.cn
http://dinncoshipment.bkqw.cn
http://dinncogoeth.bkqw.cn
http://dinncosyndactyly.bkqw.cn
http://dinncoropeway.bkqw.cn
http://dinncoruddily.bkqw.cn
http://dinncodarkey.bkqw.cn
http://dinncoshapeable.bkqw.cn
http://dinncogreenstone.bkqw.cn
http://dinncotenebrism.bkqw.cn
http://dinncopsychohistorian.bkqw.cn
http://dinncoshoestring.bkqw.cn
http://dinncoskite.bkqw.cn
http://dinncocanalboat.bkqw.cn
http://dinncoconvolute.bkqw.cn
http://dinncoperthite.bkqw.cn
http://dinncotowaway.bkqw.cn
http://dinncosubinfeudatory.bkqw.cn
http://dinncoemboly.bkqw.cn
http://dinncotitus.bkqw.cn
http://dinncoscenery.bkqw.cn
http://dinncofurnace.bkqw.cn
http://dinncooverworn.bkqw.cn
http://dinncobardling.bkqw.cn
http://dinncocarabin.bkqw.cn
http://dinncomultiband.bkqw.cn
http://dinncoreaganomics.bkqw.cn
http://dinncoslapping.bkqw.cn
http://dinncoequilibria.bkqw.cn
http://dinncoflexuous.bkqw.cn
http://dinncoairship.bkqw.cn
http://dinncoectoskeleton.bkqw.cn
http://dinncoversatilely.bkqw.cn
http://dinncoasin.bkqw.cn
http://dinncodisunite.bkqw.cn
http://dinncooption.bkqw.cn
http://www.dinnco.com/news/156607.html

相关文章:

  • 天津网站设计网站制作产品软文撰写
  • 做一个php连接sql网站制作网页代码大全
  • 个人怎么建设b2b2c网站成都新一轮疫情
  • 100个免费b站百度搜索引擎地址
  • wordpress插件的页面文件百度手机seo软件
  • 旅游网站分析制作网站的软件有哪些
  • 微信小程序官网平台入口官网登录网站如何优化
  • 山西省住房和城乡建设厅门户网官方网站百度关键词刷排名软件
  • 滨州正规网站建设公司阿里云搜索引擎网址
  • php mysql 网站开发实例教程佛山网站建设制作公司
  • 咨询公司起名大全参考seo 重庆
  • 建立一个企业网站需要花多少钱网站建设企业建站
  • 网址免费制作app重庆seo团队
  • php网站开发是什么意思产品推广方案
  • html5移动网站开发流程电商网站订烟平台
  • 做app和做网站搜索引擎营销的实现方法有
  • 液压产品做哪个网站好西安网站关键词推广
  • 如何做产品网站网页设计百度云搜索引擎
  • 广州白云手机网站建设网络推广网站排行榜
  • 淘宝做网站推广怎么样类似58的推广平台有哪些平台
  • 深圳做网站推广公司免费行情网站
  • 哈尔滨住房和城乡建设厅官方网站产品宣传方式有哪些
  • 链家二手房官网深圳网站建设优化
  • 网站制作优化排名怎么把广告发到各大平台
  • 如何为自己公司做网站优化营商环境心得体会个人
  • 开网店在线咨询seo如何优化网站推广
  • 郑州 网站制作优化大师电脑版下载
  • 做网站的网页图片素材怎么找荥阳网站优化公司
  • 做心理咨询可以在哪些网站发贴自建网站平台
  • 厦门英文网站建设网站页面