zoukankan      html  css  js  c++  java
  • leetcode — spiral-matrix-ii

    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[n]);
            }
            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));
        }
    
    }
    
  • 相关阅读:
    Python Django 编写一个简易的后台管理工具2-创建项目
    leetcode-解题记录 771. 宝石与石头
    leetcode-解题记录 1108. IP 地址无效化
    Python Django 编写一个简易的后台管理工具1-安装环境
    备忘录
    Pollard_rho 因数分解
    ProgrammingContestChallengeBook
    HDU ACM-Steps
    ARCH-LINUX 折(安)腾(装)记
    各种Python小玩意收集
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7541339.html
Copyright © 2011-2022 走看看