日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

面試官:你工作了3年了,這道算法題你都答不出來?

來源: 責編: 時間:2023-09-21 20:46:47 302觀看
導讀9月又是換工作的最佳時機。我幻想著只要換一份工作,就可以離開這個“破碎的地方”,賺更多的錢,做最舒服的事情,但事與愿違。最近,一名女學生正在換工作。面試前她準備了很多問題。我以為她很有信心,結果卻在算法上吃了大虧

9月又是換工作的最佳時機。我幻想著只要換一份工作,就可以離開這個“破碎的地方”,賺更多的錢,做最舒服的事情,但事與愿違。PBP28資訊網——每日最新資訊28at.com

PBP28資訊網——每日最新資訊28at.com

最近,一名女學生正在換工作。面試前她準備了很多問題。我以為她很有信心,結果卻在算法上吃了大虧。PBP28資訊網——每日最新資訊28at.com

什么樣的算法題能讓面試官對一個女孩說出這么狠的話:你工作了3年了,這道算法題你都解不出來?PBP28資訊網——每日最新資訊28at.com

有效括號

這是LeetCode上的一道算法題,旨在考察考生對“棧”數據結構的熟悉程度。我們來看一下。PBP28資訊網——每日最新資訊28at.com

給定一個僅包含字符‘(‘、‘)’、‘{‘、‘}’、‘[‘和‘]’的字符串 s,確定輸入字符串是否有效。PBP28資訊網——每日最新資訊28at.com

如果滿足以下條件,輸入字符串有效:開括號必須由相同類型的括號括起來。左括號必須按正確的順序關閉。PBP28資訊網——每日最新資訊28at.com

示例1:PBP28資訊網——每日最新資訊28at.com

Input: s = "()"Output: true

示例2:PBP28資訊網——每日最新資訊28at.com

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

示例3:PBP28資訊網——每日最新資訊28at.com

Input: s = "(]"Output: false

示例4:PBP28資訊網——每日最新資訊28at.com

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

實施例5:PBP28資訊網——每日最新資訊28at.com

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

限制條件:PBP28資訊網——每日最新資訊28at.com

  • 1 <= s.length <= 104
  • s 僅由括號‘()[]{}’組成

問題信息PBP28資訊網——每日最新資訊28at.com

如果我們真的沒學過算法,也不知道那么多套路,那么通過問題和例子來獲取盡可能多的信息是非常重要的。PBP28資訊網——每日最新資訊28at.com

那么,我們可以得到以下信息:PBP28資訊網——每日最新資訊28at.com

  • 字符串 s 的長度必須是偶數,不能是奇數(成對匹配)。
  • 右括號前面必須有左括號。

方法一:暴力消除法

得到以上信息后,我想既然[]、{}、()是成對出現的,那我是不是可以一一消除呢?如果最后的結果是空字符串,那不是就說明符合題意了嗎?PBP28資訊網——每日最新資訊28at.com

例如:PBP28資訊網——每日最新資訊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

代碼:PBP28資訊網——每日最新資訊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的用例,但是性能差了一點,哈哈。PBP28資訊網——每日最新資訊28at.com

方法二:使用“棧”來解決

主題信息中的第二項強調對稱性。棧(后進先出)和(推入和彈出)正好相反,形成明顯的對稱性。PBP28資訊網——每日最新資訊28at.com

例如PBP28資訊網——每日最新資訊28at.com

Input: abcOutput: cba

“abc”和“cba”是對稱的,所以我們可以嘗試從堆棧的角度來解析:PBP28資訊網——每日最新資訊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.

代碼PBP28資訊網——每日最新資訊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}

雖然暴力方案符合我們的常規思維,但是堆棧結構方案會更加高效。PBP28資訊網——每日最新資訊28at.com

最后

在面試中,算法是否應該成為評價候選人的重要指標,我們不會抱怨,但近年來,幾乎每家公司都將算法納入了前端面試中。為了拿到自己喜歡的offer,復習數據結構、刷題還是有必要的。PBP28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-10891-0.html面試官:你工作了3年了,這道算法題你都答不出來?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 一文讀懂分布式追蹤:過去、現在和未來

下一篇: CSS實現十個功能強大的一行布局技巧

標簽:
  • 熱門焦點
  • Redmi Pad評測:紅米充滿野心的一次嘗試

    從Note系列到K系列,從藍牙耳機到筆記本電腦,紅米不知不覺之間也已經形成了自己頗有競爭力的產品體系,在中端和次旗艦市場上甚至要比小米新機的表現來得更好,正所謂“大丈夫生居
  • 摸魚心法第一章——和配置文件說拜拜

    為了能摸魚我們團隊做了容器化,但是帶來的問題是服務配置文件很麻煩,然后大家在群里進行了“親切友好”的溝通圖片圖片圖片圖片對比就對比,簡單對比下獨立配置中心和k8s作為配
  • SpringBoot中使用Cache提升接口性能詳解

    環境:springboot2.3.12.RELEASE + JSR107 + Ehcache + JPASpring 框架從 3.1 開始,對 Spring 應用程序提供了透明式添加緩存的支持。和事務支持一樣,抽象緩存允許一致地使用各
  • 分享六款相見恨晚的PPT模版網站, 祝你做出精美的PPT!

    1、OfficePLUSOfficePLUS網站旨在為全球Office用戶提供豐富的高品質原創PPT模板、實用文檔、數據圖表及個性化定制服務。優點:OfficePLUS是微軟官方網站,囊括PPT模板、Word模
  • JavaScript學習 -AES加密算法

    引言在當今數字化時代,前端應用程序扮演著重要角色,用戶的敏感數據經常在前端進行加密和解密操作。然而,這樣的操作在網絡傳輸和存儲中可能會受到惡意攻擊的威脅。為了確保數據
  • 微軟邀請 Microsoft 365 商業用戶,測試視頻編輯器 Clipchamp

    8 月 1 日消息,微軟近日宣布即將面向 Microsoft 365 商業用戶,開放 Clipchamp 應用,邀請用戶通過該應用來編輯視頻。微軟于 2021 年收購 Clipchamp,隨后開始逐步整合到 Microsof
  • 雅柏威士忌多款單品價格大跌,泥煤頂流也不香了?

    來源 | 烈酒商業觀察編 | 肖海林今年以來,威士忌市場開始出現了降溫跡象,越來越多不斷暴漲的網紅威士忌也開始悄然回歸市場理性。近日,LVMH集團旗下蘇格蘭威士忌品牌雅柏(Ardbeg
  • “又被陳思誠騙了”

    作者|張思齊 出品|眾面(ID:ZhongMian_ZM)如今的國產懸疑電影,成了陳思誠的天下。最近大爆電影《消失的她》票房突破30億斷層奪魁暑期檔,陳思誠再度風頭無兩。你可以說陳思誠的
  • 回歸OPPO兩年,一加贏了銷量,輸了品牌

    成為OPPO旗下主打性能的先鋒品牌后,一加屢創佳績。今年618期間,一加手機全渠道銷量同比增長362%,憑借一加 11、一加 Ace 2、一加 Ace 2V三款爆品,一加
Top 主站蜘蛛池模板: 项城市| 石景山区| 潼南县| 凌云县| 博乐市| 拜城县| 长顺县| 始兴县| 舟曲县| 南溪县| 富锦市| 惠州市| 河间市| 渑池县| 克拉玛依市| 滦南县| 大余县| 丘北县| 叶城县| 林芝县| 皮山县| 报价| 襄樊市| 昌都县| 鄯善县| 双峰县| 谢通门县| 梅州市| 建湖县| 突泉县| 体育| 商城县| 囊谦县| 南江县| 尤溪县| 吉林省| 华坪县| 南京市| 皋兰县| 泰宁县| 夏邑县|