• 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));
        }
    
    }
    
  • 相关阅读:
    素数回文 ---- 有点暴力.....
    Manacher算法 , 实例 详解 . NYOJ 最长回文
    大数处理 详解 模版
    River Crossing 简单的动态规划 .
    hdu
    产生冠军 map 的 应用 .
    MySQL的数据库备份与恢复。
    rsync的相关使用,参数设置。
    centos与mac安装python虚拟环境 virtualenvwrapper
    CentOS7安装Python3.7
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7541339.html
Copyright © 2020-2023  润新知