成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 前端
什么樣的算法題能讓面試官對一個女孩說出這么狠的話:你工作了3年了,這道算法題你都解不出來?

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

最近,一名女學生正在換工作。面試前她準備了很多問題。我以為她很有信心,結果卻在算法上吃了大虧。

什么樣的算法題能讓面試官對一個女孩說出這么狠的話:你工作了3年了,這道算法題你都解不出來?

有效括號

這是LeetCode上的一道算法題,旨在考察考生對“棧”數據結構的熟悉程度。我們來看一下。

給定一個僅包含字符‘(‘、‘)’、‘{‘、‘}’、‘[‘和‘]’的字符串 s,確定輸入字符串是否有效。

如果滿足以下條件,輸入字符串有效:開括號必須由相同類型的括號括起來。左括號必須按正確的順序關閉。

示例1:

Input: s = "()"
Output: true

示例2:

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

示例3:

Input: s = "(]"
Output: false

示例4:

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

實施例5:

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

限制條件:

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

問題信息

如果我們真的沒學過算法,也不知道那么多套路,那么通過問題和例子來獲取盡可能多的信息是非常重要的。

那么,我們可以得到以下信息:

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

方法一:暴力消除法

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

例如:

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

代碼:

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的用例,但是性能差了一點,哈哈。

方法二:使用“?!眮斫鉀Q

主題信息中的第二項強調對稱性。棧(后進先出)和(推入和彈出)正好相反,形成明顯的對稱性。

例如

Input: abc
Output: cba

“abc”和“cba”是對稱的,所以我們可以嘗試從堆棧的角度來解析:

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.

代碼

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
}

雖然暴力方案符合我們的常規思維,但是堆棧結構方案會更加高效。

最后

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

責任編輯:華軒 來源: web前端開發
相關推薦

2020-06-17 21:22:56

Serverless面試官架構

2024-07-26 08:47:07

2015-08-13 10:29:12

面試面試官

2023-02-07 13:51:11

SQLupdate語句

2024-10-14 08:01:09

阻塞死鎖狀態

2019-12-25 11:22:19

負載均衡集群算法

2020-12-03 07:39:50

HashMap底層數據

2022-07-18 14:18:26

Babel代碼面試

2024-04-02 09:45:27

線程池Executors開發

2021-09-16 07:52:18

算法應用場景

2021-08-26 13:22:09

JVM調優參數

2023-11-27 07:37:50

面試協程池

2015-08-24 09:00:36

面試面試官

2021-11-25 10:18:42

RESTfulJava互聯網

2025-04-01 00:00:00

項目CRUD單例模式

2021-08-09 07:47:40

Git面試版本

2025-01-13 09:24:32

2021-09-01 09:44:16

Redis持久化配置

2020-07-30 07:58:36

加密算法

2023-11-10 08:44:13

分布式鎖分布式系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲美女网站 | 涩涩鲁亚洲精品一区二区 | 一级做a爰片久久毛片免费看 | 国产精品久久久久久久久久 | 在线小视频| 一区二区三区av | 亚洲精品一区二三区不卡 | 盗摄精品av一区二区三区 | 成人免费视频 | 国产在线a| 日韩在线视频观看 | 欧美一区 | 福利网址 | 成人性视频免费网站 | 亚洲一区综合 | 色在线免费视频 | 日韩福利一区 | 一区二区在线 | 久久33 | av网站在线看 | 午夜国产一区 | 成人片免费看 | 91一区| a级片在线 | 中文在线亚洲 | 日韩欧美国产综合 | 国产在线精品一区二区三区 | 精品国产精品三级精品av网址 | 99只有精品| 亚洲国产成人精品女人久久久 | 在线一区视频 | 国产亚洲精品成人av久久ww | 自拍偷拍中文字幕 | 嫩草一区二区三区 | 国产欧美日韩二区 | 日韩在线观看网站 | 国产福利在线看 | 国产在线精品一区二区三区 | 亚洲成av人影片在线观看 | 久久69精品久久久久久久电影好 | 成人免费毛片在线观看 |