一道字节笔试题,没有想到考察的是....

开发 前端
本篇分享的内容是字节的笔试题,字节的算法题。考察的点,可以归纳于深度优先遍历,或者说是一道脑筋急转弯题。

[[398239]]

大家好,我是TianTian。

分享的内容是字节的笔试题,字节的算法题。

考察的点,可以归纳于深度优先遍历,或者说是一道脑筋急转弯题。

题目给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

输入:

  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] 

思路

基本上围绕的思路就是:一层层向里处理,按顺时针依次遍历:上、右、下、左。

其实很类似于迷宫的走法,走到格子后,判断下一个格子,还能不能走,也就是边界条件。遇到边界条件后,顺着上面的顺序: 上、右、下、左。

所以我们可以有几个约束条件:

  • 是不是在这个范围内,不能超过这些范围。
  • 这个格子是不是走过,存一下之前的状态。
  • 记录当前方向,抱着下一个方向是对的。

深度优先遍历

按照深度优先遍历思路来写,我们可以构造常见的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记录是否走过 
  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. }; 

这应该就是基础的模版,唯一不同的是,我们看dfs的三个参数,x,y,directionIndex。

x和y参数很好理解,这个directionIndex参数含义就是告诉我们当前前进的方向。

接下来,是写我们的逻辑部分。首先确定接下来走到哪一个格子:

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

根据当前的格子所在的位置x,y我们就知道接下来要走的位置,通过directionIndex的下标索引,知道我们下一个格子的坐标。

然后就是判断一下,边界的情况:

  • 不能出界
  • 判断能不能走

根据以上的信息,其实我们主要的逻辑部分就完成啦。

代码:

  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.             // 判断边界 
  16.             if (nextX < col && nextX >= 0 && nextY < row && nextY >= 0 && !isCheckMatrix[nextX][nextY]) { 
  17.                 dfs(nextX, nextY, directionIndex) 
  18.             } 
  19.             // 方向取余数 
  20.             directionIndex = (directionIndex + 1) % 4; 
  21.         } 
  22.     }; 
  23.     dfs(0, 0, 0); 
  24.     return result 
  25. }; 

这里我们需要对方向做余数处理。在确只有四个方向的情况,并且在这个方向不能走的情况下,尝试下一个方向。

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

优化

写完的时候,我在想能不能优化一下,做个减枝的处理,后面发现,当前这个位置可以走的话,是不是就不能判断其他方向了。

或者说我们可以提前走出这个循环,这里做的优化就是return 当前的处理。

代码:

  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. }; 

后记

后面发现这个是一道leetcode中等的题目,题目链接:

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

 

责任编辑:姜华 来源: TianTianUp
相关推荐

2021-04-30 08:22:36

异步求和函数

2014-04-29 14:58:24

笔试题微软笔试题

2016-12-21 14:29:50

以太网数据中心服务器

2022-04-08 07:52:17

CSS面试题HTML

2011-03-07 13:29:52

NeusoftJava API

2009-06-22 13:43:00

java算法

2011-05-23 11:27:32

面试题面试java

2019-09-02 15:06:16

面试字节跳动算法

2018-03-06 15:30:47

Java面试题

2009-08-11 10:12:07

C#算法

2021-01-26 13:14:14

js前端map

2012-07-03 09:38:42

前端

2023-02-04 18:24:10

SeataJava业务

2021-05-31 07:55:44

smartRepeatJavaScript函数

2009-08-11 14:59:57

一道面试题C#算法

2017-11-21 12:15:27

数据库面试题SQL

2009-08-11 15:09:44

一道面试题C#算法

2024-03-18 13:32:11

2021-03-16 05:44:26

JVM面试题运行时数据

2023-08-01 08:10:46

内存缓存
点赞
收藏

51CTO技术栈公众号