当前位置:首页 > 科技  > 软件

面试官:你工作了3年了,这道算法题你都答不出来?

来源: 责编: 时间:2023-09-21 20:46:47 431观看
导读9月又是换工作的最佳时机。我幻想着只要换一份工作,就可以离开这个“破碎的地方”,赚更多的钱,做最舒服的事情,但事与愿违。最近,一名女学生正在换工作。面试前她准备了很多问题。我以为她很有信心,结果却在算法上吃了大亏

9月又是换工作的最佳时机。我幻想着只要换一份工作,就可以离开这个“破碎的地方”,赚更多的钱,做最舒服的事情,但事与愿违。MTp28资讯网——每日最新资讯28at.com

MTp28资讯网——每日最新资讯28at.com

最近,一名女学生正在换工作。面试前她准备了很多问题。我以为她很有信心,结果却在算法上吃了大亏。MTp28资讯网——每日最新资讯28at.com

什么样的算法题能让面试官对一个女孩说出这么狠的话:你工作了3年了,这道算法题你都解不出来?MTp28资讯网——每日最新资讯28at.com

有效括号

这是LeetCode上的一道算法题,旨在考察考生对“栈”数据结构的熟悉程度。我们来看一下。MTp28资讯网——每日最新资讯28at.com

给定一个仅包含字符‘(‘、‘)’、‘{‘、‘}’、‘[‘和‘]’的字符串 s,确定输入字符串是否有效。MTp28资讯网——每日最新资讯28at.com

如果满足以下条件,输入字符串有效:开括号必须由相同类型的括号括起来。左括号必须按正确的顺序关闭。MTp28资讯网——每日最新资讯28at.com

示例1:MTp28资讯网——每日最新资讯28at.com

Input: s = "()"Output: true

示例2:MTp28资讯网——每日最新资讯28at.com

Input: s = "()[]{}"Output: true

示例3:MTp28资讯网——每日最新资讯28at.com

Input: s = "(]"Output: false

示例4:MTp28资讯网——每日最新资讯28at.com

Input: s = "([)]"Output: false

实施例5:MTp28资讯网——每日最新资讯28at.com

Input: s = "{[]}"Output: true

限制条件:MTp28资讯网——每日最新资讯28at.com

  • 1 <= s.length <= 104
  • s 仅由括号‘()[]{}’组成

问题信息MTp28资讯网——每日最新资讯28at.com

如果我们真的没学过算法,也不知道那么多套路,那么通过问题和例子来获取尽可能多的信息是非常重要的。MTp28资讯网——每日最新资讯28at.com

那么,我们可以得到以下信息:MTp28资讯网——每日最新资讯28at.com

  • 字符串 s 的长度必须是偶数,不能是奇数(成对匹配)。
  • 右括号前面必须有左括号。

方法一:暴力消除法

得到以上信息后,我想既然[]、{}、()是成对出现的,那我是不是可以一一消除呢?如果最后的结果是空字符串,那不是就说明符合题意了吗?MTp28资讯网——每日最新资讯28at.com

例如:MTp28资讯网——每日最新资讯28at.com

Input: s = "{[()]}"Step 1: The pair of () can be eliminated, and the result s is left with {[]}Step 2: The pair of [] can be eliminated, and the result s is left with {}Step 3: The pair of {} can be eliminated, and the result s is left with '', so it returns true in line with the meaning of the question

代码:MTp28资讯网——每日最新资讯28at.com

const isValid = (s) => {  while (true) {    let len = s.length    // Replace the string with '' one by one according to the matching pair    s = s.replace('{}', '').replace('[]', '').replace('()', '')    // There are two cases where s.length will be equal to len    // 1. s is matched and becomes an empty string    // 2. s cannot continue to match, so its length is the same as the len at the beginning, for example ({], len is 3 at the beginning, and it is still 3 after matching, indicating that there is no need to continue matching, and the result is false    if (s.length === len) {      return len === 0    }  }}

暴力消除方式还是可以通过LeetCode的用例,但是性能差了一点,哈哈。MTp28资讯网——每日最新资讯28at.com

方法二:使用“栈”来解决

主题信息中的第二项强调对称性。栈(后进先出)和(推入和弹出)正好相反,形成明显的对称性。MTp28资讯网——每日最新资讯28at.com

例如MTp28资讯网——每日最新资讯28at.com

Input: abcOutput: cba

“abc”和“cba”是对称的,所以我们可以尝试从堆栈的角度来解析:MTp28资讯网——每日最新资讯28at.com

Input: s = "{[()]}"Step 1: read ch = {, which belongs to the left bracket, and put it into the stack. At this time, there is { in the stack.Step 2: Read ch = [, which belongs to the left parenthesis, and push it into the stack. At this time, there are {[ in the stack.Step 3: read ch = (, which belongs to the left parenthesis, and push it into the stack. At this time, there are {[( in the stack.Step 4: Read ch = ), which belongs to the right parenthesis, try to read the top element of the stack (and ) just match, and pop ( out of the stack, at this time there are {[.Step 5: Read ch = ], which belongs to the right parenthesis, try to read the top element of the stack [and ] just match, pop the [ out of the stack, at this time there are {.Step 6: Read ch = }, which belongs to the right parenthesis, try to read the top element of the stack { and } exactly match, pop { out of the stack, at this time there is still '' in the stack.Step 7: There is only '' left in the stack, s = "{[()]}" conforms to the valid bracket definition and returns true.

代码MTp28资讯网——每日最新资讯28at.com

const isValid = (s) => {  // The empty string character is valid  if (!s) {    return true  }  const leftToRight = {    '(': ')',    '[': ']',    '{': '}'  }  const stack = []  for (let i = 0, len = s.length; i < len; i++) {    const ch = s[i]    // Left parenthesis    if (leftToRight[ch]) {      stack.push(ch)    } else {      // start matching closing parenthesis      // 1. If there is no left parenthesis in the stack, directly false      // 2. There is data but the top element of the stack is not the current closing parenthesis      if (!stack.length || leftToRight[ stack.pop() ] !== ch) {        return false      }    }  }  // Finally check if the stack is empty  return !stack.length}

虽然暴力方案符合我们的常规思维,但是堆栈结构方案会更加高效。MTp28资讯网——每日最新资讯28at.com

最后

在面试中,算法是否应该成为评价候选人的重要指标,我们不会抱怨,但近年来,几乎每家公司都将算法纳入了前端面试中。为了拿到自己喜欢的offer,复习数据结构、刷题还是有必要的。MTp28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-10891-0.html面试官:你工作了3年了,这道算法题你都答不出来?

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 一文读懂分布式追踪:过去、现在和未来

下一篇: CSS实现十个功能强大的一行布局技巧

标签:
  • 热门焦点
  • MIX Fold3包装盒泄露 新机本月登场

    小米的全新折叠屏旗舰MIX Fold3将于本月发布,近日该机的真机包装盒在网上泄露。从图上来看,新的MIX Fold3包装盒在外观设计方面延续了之前的方案,变化不大,这也是目前小米旗舰
  • 6月安卓手机好评榜:魅族20 Pro蝉联冠军

    性能榜和性价比榜之后,我们来看最后的安卓手机好评榜,数据来源安兔兔评测,收集时间2023年6月1日至6月30日,仅限国内市场。第一名:魅族20 Pro好评率:95%5月份的时候魅族20 Pro就是
  • Automa-通过连接块来自动化你的浏览器

    1、前言通过浏览器插件可实现自动化脚本的录制与编写,具有代表性的工具就是:Selenium IDE、Katalon Recorder,对于简单的业务来说可快速实现自动化的上手工作。Selenium IDEKat
  • 之家push系统迭代之路

    前言在这个信息爆炸的互联网时代,能够及时准确获取信息是当今社会要解决的关键问题之一。随着之家用户体量和内容规模的不断增大,传统的靠"主动拉"获取信息的方式已不能满足用
  • 东方甄选单飞:有些鸟注定是关不住的

    文/彭宽鸿编辑/罗卿东方甄选创始人俞敏洪带队的&ldquo;7天甘肃行&rdquo;直播活动已在近日顺利收官。成立后一年多时间里,东方甄选要脱离抖音自立门户的传闻不绝于耳,&ldquo;7
  • 小米汽车电池信息疑似曝光:容量101kWh,支持800V高压快充

    7月14日消息,今日一名博主在社交媒体发布了一张疑似小米汽车电池信息的照片,显示该电池包正是宁德时代麒麟电池,容量为101kWh,电压为726.7V,可以预测小
  • 苹果、三星、惠普等暂停向印度出口笔记本和平板电脑

    集微网消息,据彭博社报道,在8月3日印度突然禁止在没有许可证的情况下向印度进口电脑/平板及显示器等产品后,苹果、三星电子和惠普等大公司暂停向印度
  • 苹果MacBook Pro 2021测试:仍不支持平滑滚动

    据10月30日9to5 Mac 消息报道,苹果新的 14 英寸和 16 英寸 MacBook Pro 2021 上市后获得了不错的评价,亮点包括行业领先的性能,令人印象深刻的电池续航,精美丰
  • 电博会与软博会实现"线下+云端"的双线融合

    在本次“电博会”与“软博会”双展会利好条件的加持下,既可以发挥展会拉动人流、信息流、资金流实现快速交互流动的作用,继而推动区域经济良性发展;又可以聚
Top