leetcode — spiral-matrix-ii
2017-09-18 08:04
489 查看
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://oj.leetcode.com/problems/spiral-matrix-ii/ * * Created by lverpeng on 2017/7/19. * * Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. * * For example, * Given n = 3, * * You should return the following matrix: * * [ * [ 1, 2, 3 ], * [ 8, 9, 4 ], * [ 7, 6, 5 ] * ] * */ public class SpiralMatrix2 { public List<Integer[]> build (int n) { List<Integer[]> result = new ArrayList<Integer[]>(); for (int i = 0; i < n; i++) { result.add(new Integer ); } int row = 0; int col = 0; int count = 1; for (; col < (n+1) / 2 && row < (n+1) / 2; row ++, col++) { for (int i = col; i < n - col; i++) { result.get(row)[i] = count++; } for (int i = row + 1; i < n - row; i ++) { result.get(i)[n - col - 1] = count++; } for (int i = n - col - 2; i >= col; i--) { result.get(n - row - 1)[i] = count++; } for (int i = n - row - 2; i > row; i--) { result.get(i)[col] = count++; } } return result; } public static void print (List<Integer[]> list) { for (Integer[] arr : list) { System.out.println(Arrays.toString(arr)); } System.out.println(); } public static void main(String[] args) { SpiralMatrix2 spiralMatrix2 = new SpiralMatrix2(); print(spiralMatrix2.build(0)); print(spiralMatrix2.build(1)); print(spiralMatrix2.build(2)); print(spiralMatrix2.build(3)); print(spiralMatrix2.build(4)); print(spiralMatrix2.build(5)); } }
相关文章推荐
- LeetCode 59. Spiral Matrix II
- LeetCode 58 Spiral Matrix II
- Spiral Matrix II 产生正方形的旋转矩阵@LeetCode
- 【leetcode】Array——Spiral MatrixII(59)
- leetcode-59 Spiral Matrix II
- leetcode[59]Spiral Matrix II
- leetcode: Spiral Matrix II
- [LeetCode] Spiral Matrix II
- LeetCode-Spiral Matrix II
- LeetCode Spiral Matrix II
- leetcode-Spiral Matrix II
- [Leetcode] Spiral Matrix II (Java)
- [Leetcode] 59. Spiral Matrix II
- LEETCODE: Spiral Matrix II
- [LeetCode] 59. Spiral Matrix II
- leetcode_59题——Spiral Matrix II(数组)
- 【leetcode】59. Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- 【LeetCode】Spiral Matrix II
- leetCode->Spiral Matrix II