LeetCode059——螺旋矩陣II
阿新 • • 發佈:2018-12-11
題目描述:
知識點:陣列
思路:實時更新螺旋的四個邊界left、right、top、bottom的值
JAVA程式碼:
public class Solution { public int[][] generateMatrix(int n) { int[][] result = new int[n][n]; int num = 1; int left = 0; int right = n - 1; int top = 0; int bottom = n - 1; while(left <= right && top <= bottom) { for (int i = left; i <= right; i++) { result[top][i] = num++; } top++; for (int i = top; i <= bottom; i++) { result[i][right] = num++; } right--; if(top <= bottom) { for (int i = right; i >= left; i--) { result[bottom][i] = num++; } bottom--; } if(left <= right) { for (int i = bottom; i >= top; i--) { result[i][left] = num++; } left++; } } return result; } }
LeetCode解題報告: