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

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

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

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

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

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

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

有效括号

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

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

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

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

Input: s = "()"Output: true

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

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

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

Input: s = "(]"Output: false

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

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

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

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

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

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

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

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

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

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

方法一:暴力消除法

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

例如:dHU28资讯网——每日最新资讯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

代码:dHU28资讯网——每日最新资讯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的用例,但是性能差了一点,哈哈。dHU28资讯网——每日最新资讯28at.com

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

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

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

Input: abcOutput: cba

“abc”和“cba”是对称的,所以我们可以尝试从堆栈的角度来解析:dHU28资讯网——每日最新资讯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.

代码dHU28资讯网——每日最新资讯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}

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

最后

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

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

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

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

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

标签:
  • 热门焦点
  • 2023年Q2用户偏好榜:12+256G版本成新主流

    3月份的性能榜、性价比榜和好评榜之后,就要轮到2023年的第二季度偏好榜了,上半年的新机潮已经过去,最明显的肯定就是大内存和存储的机型了,另外部分中端机也取消了屏幕塑料支架
  • 如何正确使用:Has和:Nth-Last-Child

    我们可以用CSS检查,以了解一组元素的数量是否小于或等于一个数字。例如,一个拥有三个或更多子项的grid。你可能会想,为什么需要这样做呢?在某些情况下,一个组件或一个布局可能会
  • 一篇聊聊Go错误封装机制

    %w 是用于错误包装(Error Wrapping)的格式化动词。它是用于 fmt.Errorf 和 fmt.Sprintf 函数中的一个特殊格式化动词,用于将一个错误(或其他可打印的值)包装在一个新的错误中。使
  • 一文搞定Java NIO,以及各种奇葩流

    大家好,我是哪吒。很多朋友问我,如何才能学好IO流,对各种流的概念,云里雾里的,不求甚解。用到的时候,现百度,功能虽然实现了,但是为什么用这个?不知道。更别说效率问题了~下次再遇到,
  • 小红书1周涨粉49W+,我总结了小白可以用的N条涨粉笔记

    作者:黄河懂运营一条性教育视频,被54万人&ldquo;珍藏&rdquo;是什么体验?最近,情感博主@公主是用鲜花做的,火了!仅仅凭借一条视频,光小红书就有超过128万人,为她疯狂点赞!更疯狂的是,这
  • 2023年,我眼中的字节跳动

    此时此刻(2023年7月),字节跳动从未上市,也从未公布过任何官方的上市计划;但是这并不妨碍它成为中国最受关注的互联网公司之一。从2016-17年的抖音强势崛起,到2018年的&ldquo;头腾
  • 三星电子Q2营收60万亿韩元 存储业务营收同比仍下滑超过50%

    7月27日消息,据外媒报道,从三星电子所发布的财报来看,他们主要利润来源的存储芯片业务在今年二季度仍不乐观,营收同比仍在大幅下滑,所在的设备解决方案
  • 三星Galaxy Z Fold5官方渲染图曝光:13.4mm折叠厚度依旧感人

    据官方此前宣布,三星将于7月26日在韩国首尔举办Unpacked活动,届时将带来带来包括Galaxy Buds 3、Galaxy Watch 6、Galaxy Tab S9、Galaxy Z Flip 5、
  • iQOO Neo8 Pro抢先上架:首发天玑9200+ 安卓性能之王

    经过了一段时间的密集爆料,昨日iQOO官方如期对外宣布:将于5月23日推出全新的iQOO Neo8系列新品,官方称这是一款拥有旗舰级性能调校的作品。随着发布时
Top