自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一道字節(jié)筆試題,沒(méi)有想到考察的是....

開(kāi)發(fā) 前端
本篇分享的內(nèi)容是字節(jié)的筆試題,字節(jié)的算法題??疾斓狞c(diǎn),可以歸納于深度優(yōu)先遍歷,或者說(shuō)是一道腦筋急轉(zhuǎn)彎題。

[[398239]]

大家好,我是TianTian。

分享的內(nèi)容是字節(jié)的筆試題,字節(jié)的算法題。

考察的點(diǎn),可以歸納于深度優(yōu)先遍歷,或者說(shuō)是一道腦筋急轉(zhuǎn)彎題。

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

輸入:

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

輸出:

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

思路

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

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

所以我們可以有幾個(gè)約束條件:

  • 是不是在這個(gè)范圍內(nèi),不能超過(guò)這些范圍。
  • 這個(gè)格子是不是走過(guò),存一下之前的狀態(tài)。
  • 記錄當(dāng)前方向,抱著下一個(gè)方向是對(duì)的。

深度優(yōu)先遍歷

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

  1. const spiralOrder = function (matrix) { 
  2.     if (matrix.length === 0) return []; 
  3.     const result = [], 
  4.         dx = [0, 1, 0, -1], 
  5.         dy = [1, 0, -1, 0], 
  6.         col = matrix.length, 
  7.         row = matrix[0].length; 
  8.     // isCheckMatrix記錄是否走過(guò) 
  9.     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false)))  
  10.     const dfs = (x, y, directionIndex) => { 
  11.           // 邏輯代碼 
  12.             // 通常這里做邏輯處理,邊界處理 
  13.         } 
  14.     }; 
  15.     dfs(0, 0, 0); 
  16.     return result 
  17. }; 

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

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

接下來(lái),是寫我們的邏輯部分。首先確定接下來(lái)走到哪一個(gè)格子:

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

根據(jù)當(dāng)前的格子所在的位置x,y我們就知道接下來(lái)要走的位置,通過(guò)directionIndex的下標(biāo)索引,知道我們下一個(gè)格子的坐標(biāo)。

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

  • 不能出界
  • 判斷能不能走

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

代碼:

  1. const spiralOrder = function (matrix) { 
  2.     if (matrix.length === 0) return []; 
  3.     const result = [], 
  4.         dx = [0, 1, 0, -1], 
  5.         dy = [1, 0, -1, 0], 
  6.         col = matrix.length, 
  7.         row = matrix[0].length; 
  8.     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false))) 
  9.     const dfs = (x, y, directionIndex) => { 
  10.         result.push(matrix[x][y]) // 存答案 
  11.         isCheckMatrix[x][y] = true // 標(biāo)記走過(guò) 
  12.         for (let i = 0; i < 3; i++) { 
  13.             const nextX = x + dx[directionIndex] 
  14.             const nextY = y + dy[directionIndex] 
  15.             // 判斷邊界 
  16.             if (nextX < col && nextX >= 0 && nextY < row && nextY >= 0 && !isCheckMatrix[nextX][nextY]) { 
  17.                 dfs(nextX, nextY, directionIndex) 
  18.             } 
  19.             // 方向取余數(shù) 
  20.             directionIndex = (directionIndex + 1) % 4; 
  21.         } 
  22.     }; 
  23.     dfs(0, 0, 0); 
  24.     return result 
  25. }; 

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

  1. directionIndex = (directionIndex + 1) % 4; 

優(yōu)化

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

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

代碼:

  1. const spiralOrder = function (matrix) { 
  2.     if (matrix.length === 0) return []; 
  3.     const result = [], 
  4.         dx = [0, 1, 0, -1], 
  5.         dy = [1, 0, -1, 0], 
  6.         col = matrix.length, 
  7.         row = matrix[0].length; 
  8.     const isCheckMatrix = Array.from(new Array(col), () => (new Array(row).fill(false))) 
  9.     const dfs = (x, y, directionIndex) => { 
  10.         result.push(matrix[x][y]); 
  11.         isCheckMatrix[x][y] = true 
  12.         for (let i = 0; i < 3; i++) { 
  13.             const nextX = x + dx[directionIndex] 
  14.             const nextY = y + dy[directionIndex] 
  15.             if (nextX < col && nextX >= 0 && nextY < row && nextY >= 0 && !isCheckMatrix[nextX][nextY]) { 
  16.                 return dfs(nextX, nextY, directionIndex) 
  17.             } 
  18.             directionIndex = (directionIndex + 1) % 4; 
  19.         } 
  20.     }; 
  21.     dfs(0, 0, 0); 
  22.     return result 
  23. }; 

后記

后面發(fā)現(xiàn)這個(gè)是一道leetcode中等的題目,題目鏈接:

螺旋矩陣: https://leetcode-cn.com/problems/spiral-matrix/

 

責(zé)任編輯:姜華 來(lái)源: TianTianUp
相關(guān)推薦

2021-04-30 08:22:36

異步求和函數(shù)

2014-04-29 14:58:24

筆試題微軟筆試題

2016-12-21 14:29:50

以太網(wǎng)數(shù)據(jù)中心服務(wù)器

2022-04-08 07:52:17

CSS面試題HTML

2011-03-07 13:29:52

NeusoftJava API

2024-10-11 17:09:27

2009-06-22 13:43:00

java算法

2011-05-23 11:27:32

面試題面試java

2019-09-02 15:06:16

面試字節(jié)跳動(dòng)算法

2018-03-06 15:30:47

Java面試題

2009-08-11 10:12:07

C#算法

2023-02-04 18:24:10

SeataJava業(yè)務(wù)

2012-07-03 09:38:42

前端

2021-01-26 13:14:14

js前端map

2009-08-11 14:59:57

一道面試題C#算法

2021-05-31 07:55:44

smartRepeatJavaScript函數(shù)

2009-08-11 15:09:44

一道面試題C#算法

2017-11-21 12:15:27

數(shù)據(jù)庫(kù)面試題SQL

2024-03-18 13:32:11

2023-08-01 08:10:46

內(nèi)存緩存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)