# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param matrix int整型二维数组 # @return int整型一维数组 # class Solution: def SpiralMatrix(self , matrix ): # write code here # 1 2 3 4 # 5 6 7 8 # 9 10 11 12 if not matrix: return [] if len(matrix) and not len(matrix[0]):return [] if len(matrix)==1:return matrix[0] #res初始化 res = [0 for _ in range(len(matrix)*len(matrix[0]))] #初始化边界 up_bound, left_bound, down_bound, right_bound = 0, 0, len(matrix)-1, len(matrix[0])-1 #打印并收缩边界 idx = 0 while True: #向右 for col in range(left_bound,right_bound+1): res[idx] = matrix[up_bound][col] idx+=1 up_bound+=1 if up_bound>down_bound:break #向下 for row in range(up_bound,down_bound+1): res[idx] = matrix[row][right_bound] idx += 1 right_bound-=1 if left_bound > right_bound:break #向左 for col in range(right_bound,left_bound-1,-1): res[idx] = matrix[down_bound][col] idx += 1 down_bound -= 1 if up_bound > down_bound:break #向上 for row in range(down_bound,up_bound-1,-1): res[idx] = matrix[row][left_bound] idx+=1 left_bound+=1 if left_bound>right_bound:break return res
import java.util.*; public class Solution { public int[] SpiralMatrix (int[][] matrix) { int m=matrix.length,n=matrix[0].length; int index=0; int l=0,r=n-1,t=0,b=m-1;//左右上下边界 int[] res=new int[m*n]; while(true){ //从左往右 for(int i=l;i<=r;i++)res[index++]=matrix[t][i]; if(++t>b) break; //从上往下 for(int i=t;i<=b;i++) res[index++]=matrix[i][r]; if(--r<l) break; //从左往右 for(int i=r;i>=l;i--) res[index++]=matrix[b][i]; if(--b<t) break; //从下往上 for(int i=b;i>=t;i--) res[index++]=matrix[i][l]; if(++l>r)break; } return res; } }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param matrix int整型二维数组 # @return int整型一维数组 # class Solution: def SpiralMatrix(self, matrix): if not matrix: return [] res = [] while matrix: # 取出第一行内容加入到 res 中 res += matrix.pop(0) # 添加完成后如果还留有内容且不为空 if matrix and matrix[0]: # 遍历行,取出最后一个元素加到 res 中 for row in matrix: res.append(row.pop()) # 如果依旧有内容,选择最后一行,倒序加入 res 中 if matrix: res += matrix.pop()[::-1] # 如果依旧有内容且不为空,则倒序遍历列,将第0个元素加入到 res 中 if matrix and matrix[0]: for row in matrix[::-1]: res.append(row.pop(0)) return res
function SpiralMatrix( matrix ) { // write code here // 矩阵为空的情况 if(matrix.length === 0) return [] // 定义边界条件,分别是四个方向上下左右 let top = 0 let bottom = matrix.length - 1 let left = 0 let right = matrix[0].length - 1 // 旋转方向 let direction = "right" // 定义一个接收答案的数组 let result = [] // 开始循环的条件,方向是右下左上 while(left<=right && top<=bottom){ // 方向向右时 if(direction === "right"){ for(let i=left;i<=right;i++){ // 循环遍历矩阵的top行 result.push(matrix[top][i]) // 将矩阵的top行中的每一个元素都push到result数组 } top++ direction = "down" // 将方向改为向下 } // 方向向下时 else if(direction === "down"){ for(let i=top;i<=bottom;i++){ // 循环遍历矩阵的right列 result.push(matrix[i][right]) // 将矩阵的right列中的每一个元素都push到result数组 } right-- direction = "left" // 将方向改为向左 } // 方向向左时 else if(direction === "left"){ for(let i=right;i>=left;i--){ result.push(matrix[bottom][i]) } bottom-- direction = "top" // 将方向改为向上 } // 方向向右时 else if(direction === "top"){ for(let i=bottom;i>=top;i--){ result.push(matrix[i][left]) } left++ direction = "right" // 将方向改为向右 } } return result }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param matrix int整型vector<vector<>> * @return int整型vector */ vector<int> SpiralMatrix(vector<vector<int> >& matrix) { vector<int> res; if (matrix.size() == 0 || matrix[0].size() == 0) { return res; } int row = matrix.size(), col = matrix[0].size(); int left = 0, right = col - 1, top = 0, bottom = row - 1; while (left <= right && top <= bottom) { for (int j = left; j <= right; j++) res.push_back(matrix[top][j]); for (int i = top + 1; i <= bottom; i++) res.push_back(matrix[i][right]); if (left < right && top < bottom) { for (int j = right - 1; j > left; j--) res.push_back(matrix[bottom][j]); for (int i = bottom; i > top; i--) res.push_back(matrix[i][left]); } ++left; --right; ++top; --bottom; } return res; } };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param matrix int整型二维数组 * @return int整型一维数组 */ public int[] SpiralMatrix (int[][] matrix) { // write code here int m=matrix.length; int n=matrix[0].length; int[] array=new int[m*n]; int now=0,col=0,index=0; int[][] arr={{0,1},{1,0},{0,-1},{-1,0}};//记录数组 int[][] use=new int[m][n]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ use[i][j]=0; } } for(int i=0;i<m*n;i++){ array[i]=matrix[now][col]; use[now][col]=1; int nextnow=now+arr[index][0]; int nextcol=col+arr[index][1]; if(nextcol<0||nextcol>=n||nextnow>=m||use[nextnow][nextcol]==1){ index=(index+1)%4; } now+=arr[index][0]; col+=arr[index][1]; } return array; } }
func SpiralMatrix( matrix [][]int ) []int { // write code here var rtArray []int; var endRow int = len(matrix) -1 endCol := len(matrix[0]) -1 startCol := 0 startRow := 0 for { if (startRow > endRow) || (startCol > endCol) { break } for i := startCol; i < endCol + 1; i++{ rtArray = append(rtArray, matrix[startRow][i] ) } startRow += 1 if (startRow > endRow) || (startCol > endCol) { break } for i := startRow; i < endRow + 1; i++{ rtArray = append(rtArray, matrix[i][endCol] ) } endCol -= 1 if (startRow > endRow) || (startCol > endCol) { break } for i := endCol; i > startCol - 1; i-- { rtArray = append(rtArray, matrix[endRow][i] ) } endRow -= 1 if startRow > endRow || startCol > endCol { break } for i := endRow; i > startRow - 1; i-- { rtArray = append(rtArray, matrix[i][startCol] ) } startCol += 1 } return rtArray }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param matrix int整型二维数组 * @return int整型一维数组 */ public int[] SpiralMatrix (int[][] matrix) { // write code here int m = matrix.length;//m行 int n = matrix[0].length;//n列 int up = 0;//上指针,索引 int down = m-1;//下指针,索引 int left = 0;//左指针,索引 int right = n-1;//右指针,索引 int[] result = new int[m*n]; int index = 0; while(true) { //从左往右,遍历到右指针处 for (int i =left; i<= right;i++) { result[index++] = matrix[up][i]; } //上指针向下移动,++ if (up++ == down) { break; } //从上往下遍历,遍历到下指针处 for (int i = up;i <= down;i++) { result[index++] = matrix[i][right]; } //右指针左移动,-- if (right-- == left) { break; } //从右边往左边遍历,遍历到左指针处 for (int i = right; i >=left;i--) { result[index++] = matrix[down][i]; } //下指针上移;-- if (down-- == up) { break; } //从下往上遍历,遍历到上指针处 for (int i = down; i >= up;i--) { result[index++] = matrix[i][left]; } //左指针右移;++ if (left++ == right) { break; } } return result; } }