溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

前端面試中字節(jié)的筆試題和算法題示例分析

發(fā)布時間:2021-09-17 10:34:09 來源:億速云 閱讀:220 作者:柒染 欄目:web開發(fā)

這篇文章將為大家詳細講解有關字節(jié)的筆試題和算法題示例分析,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

題目給定一個包含 m x n 個元素的矩陣(m 行, n 列),請按照順時針螺旋順序,返回矩陣中的所有元素。

輸入:

[    [ 1, 2, 3 ],    [ 4, 5, 6 ],    [ 7, 8, 9 ]  ]

輸出:

[1,2,3,6,9,8,7,4,5]

思路

基本上圍繞的思路就是:一層層向里處理,按順時針依次遍歷:上、右、下、左。

其實很類似于迷宮的走法,走到格子后,判斷下一個格子,還能不能走,也就是邊界條件。遇到邊界條件后,順著上面的順序: 上、右、下、左。

所以我們可以有幾個約束條件:

  • 是不是在這個范圍內(nèi),不能超過這些范圍。

  • 這個格子是不是走過,存一下之前的狀態(tài)。

  • 記錄當前方向,抱著下一個方向是對的。

深度優(yōu)先遍歷

按照深度優(yōu)先遍歷思路來寫,我們可以構(gòu)造常見的dfs模版:

const spiralOrder = function (matrix) {     if (matrix.length === 0) return [];     const result = [],         dx = [0, 1, 0, -1],         dy = [1, 0, -1, 0],         col = matrix.length,         row = matrix[0].length;     // isCheckMatrix記錄是否走過     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false)))      const dfs = (x, y, directionIndex) => {           // 邏輯代碼             // 通常這里做邏輯處理,邊界處理         }     };     dfs(0, 0, 0);     return result };

這應該就是基礎的模版,唯一不同的是,我們看dfs的三個參數(shù),x,y,directionIndex。

x和y參數(shù)很好理解,這個directionIndex參數(shù)含義就是告訴我們當前前進的方向。

接下來,是寫我們的邏輯部分。首先確定接下來走到哪一個格子:

dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] const nextX = x + dx[directionIndex] const nextY = y + dy[directionIndex]

根據(jù)當前的格子所在的位置x,y我們就知道接下來要走的位置,通過directionIndex的下標索引,知道我們下一個格子的坐標。

然后就是判斷一下,邊界的情況:

  • 不能出界

  • 判斷能不能走

根據(jù)以上的信息,其實我們主要的邏輯部分就完成啦。

代碼:

const spiralOrder = function (matrix) {     if (matrix.length === 0) return [];     const result = [],         dx = [0, 1, 0, -1],         dy = [1, 0, -1, 0],         col = matrix.length,         row = matrix[0].length;     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false)))     const dfs = (x, y, directionIndex) => {         result.push(matrix[x][y]) // 存答案         isCheckMatrix[x][y] = true // 標記走過         for (let i = 0; i < 3; i++) {             const nextX = x + dx[directionIndex]             const nextY = y + dy[directionIndex]             // 判斷邊界             if (nextX < col && nextX >= 0 && nextY < row && nextY >= 0 && !isCheckMatrix[nextX][nextY]) {                 dfs(nextX, nextY, directionIndex)             }             // 方向取余數(shù)             directionIndex = (directionIndex + 1) % 4;         }     };     dfs(0, 0, 0);     return result };

這里我們需要對方向做余數(shù)處理。在確只有四個方向的情況,并且在這個方向不能走的情況下,嘗試下一個方向。

directionIndex = (directionIndex + 1) % 4;

優(yōu)化

寫完的時候,我在想能不能優(yōu)化一下,做個減枝的處理,后面發(fā)現(xiàn),當前這個位置可以走的話,是不是就不能判斷其他方向了。

或者說我們可以提前走出這個循環(huán),這里做的優(yōu)化就是return 當前的處理。

代碼:

const spiralOrder = function (matrix) {     if (matrix.length === 0) return [];     const result = [],         dx = [0, 1, 0, -1],         dy = [1, 0, -1, 0],         col = matrix.length,         row = matrix[0].length;     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false)))     const dfs = (x, y, directionIndex) => {         result.push(matrix[x][y]);         isCheckMatrix[x][y] = true         for (let i = 0; i < 3; i++) {             const nextX = x + dx[directionIndex]             const nextY = y + dy[directionIndex]             if (nextX < col && nextX >= 0 && nextY < row && nextY >= 0 && !isCheckMatrix[nextX][nextY]) {                 return dfs(nextX, nextY, directionIndex)             }             directionIndex = (directionIndex + 1) % 4;         }     };     dfs(0, 0, 0);     return result };

關于字節(jié)的筆試題和算法題示例分析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI