zoukankan      html  css  js  c++  java
  • 59. Spiral Matrix II

    题目:

    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 ]
    ]
    

    链接: http://leetcode.com/problems/spiral-matrix-ii/

    题解:

    和spiral matrix I基本一样,这回事生成一个spiral matrix。最近的问题是,题目不难,但要提高做题速度和准确度,这样才有时间能学习别的知识。

    要学习和想学习的东西很多很多,像多线程,软件测试,QA, Python,Javascript,Design Patterns, OO Design, System Design, Operating Systems, Distributed Systems,Data Mining, Machine Learning以及真题。继续努力吧,刷题只是很小的一部分,不过连题也刷不顺的话,是没机会进好公司的。 

    还要好好学习学习时间管理,时间真的不够用。

    Time Complexity - O(n * n), Space Complexity - O(1)。

    public class Solution {
        public int[][] generateMatrix(int n) {
            if(n <= 0)
                return new int[0][0];
            int[][] res = new int[n][n];
            int count = 1;
            int left = 0, right = n - 1, top = 0, bot = n - 1;
            
            while(count <= n * n) {
                for(int i = left; i <= right; i++)
                    res[top][i] = count++;
                top++;
                
                if(count <= n * n) {
                    for(int i = top; i <= bot; i++)
                        res[i][right] = count++;
                    right--;
                }
                
                if(count <= n * n) {
                    for(int i = right; i >= left; i--)
                        res[bot][i] = count++;
                    bot--;
                }
                
                if(count <= n* n) {
                    for(int i = bot; i >= top; i--)
                        res[i][left] = count++;
                    left++;
                }
            }
            
            return res;
        }
    }

    二刷:

    跟一刷一样,也跟Spiral Matrix I一样,设置四个边界点然后一直转圈赋值就可以了。

    Java:

    Time Complexity - O(n * n), Space Complexity - O(1)

    public class Solution {
        public int[][] generateMatrix(int n) {
            if (n <= 0) {
                return new int[][] {};
            }
            int[][] res = new int[n][n];
            int totalElements = n * n, count = 0;
            
            int left = 0, right = n -1, top = 0, bot = n - 1;
            while (count < totalElements) {
                for (int i = left; i <= right; i++) {
                    res[top][i] = count + 1;
                    count++;
                }
                top++;
                if (count < totalElements) {
                    for (int i = top; i <= bot; i++) {
                        res[i][right] = count + 1;
                        count++;
                    }
                    right--;
                }
                if (count < totalElements) {
                    for (int i = right; i >= left; i--) {
                        res[bot][i] = count + 1;
                        count++;
                    }
                    bot--;
                }
                if (count < totalElements) {
                    for (int i = bot; i >= top; i--) {
                        res[i][left] = count + 1;
                        count++;
                    }
                    left++;
                }
            }
            return res;
        }
    }

    题外话:

    1/30/2016

    今天蘑菇回国,很想念她,希望咳嗽早点好,然后多吃多玩,好好放松休息吧。

    这两天群里的小伙伴们讨论得很有干劲,但有不少朋友可能是三分钟热度,希望能持之以恒,一起加油。另外,看到地理说刷了5遍还没找到工作以及刷了4遍还没找到实习的...压力山大啊...

    发现了一本好书<Big Data: Principles and best practices of scalable realtime data systems>。 有机会要好好读一读。自己系统设计,包括一般设计方面的技术,思路等等都比较差。这就是以前几年沉溺在温床中,不思进取混日子的代价。要多思考多练习,多参加一些tech talk,不要总做井底之蛙。

    三刷:

    跟前面一样。就是先确定好n x n矩阵,以及总元素 totalElements = n * n。 设置一个count = 1,在count <= totalElements的情况下进行转圈赋值。

    Java:

    public class Solution {
        public int[][] generateMatrix(int n) {
            if (n <= 0) return new int[][] {};
            int[][] matrix = new int[n][n];
            int left = 0, right = n - 1, top = 0, bot = n - 1;
            int count = 1, totalElements = n * n;
            while (count <= totalElements) {
                for (int i = left; i <= right; i++) matrix[top][i] = count++;
                top++;
                if (count <= totalElements) {
                    for (int i = top; i <= bot; i++) matrix[i][right] = count++;
                    right--;
                }
                if (count <= totalElements) {
                    for (int i = right; i >= left; i--) matrix[bot][i] = count++;
                    bot--;
                }
                if (count <= totalElements) {
                    for (int i = bot; i >= top; i--) matrix[i][left] = count++;
                    left++;
                }
            }
            return matrix;
        }
    }

    相关题目:

    http://www.cnblogs.com/yrbbest/p/5165084.html

  • 相关阅读:
    cookie和session详解
    MacOS Sierra10.12.4编译Android7.1.1源代码必须跳的坑
    LeetCode——4Sum &amp; 总结
    C#深拷贝
    iOS9中怎样注冊远程通知
    hdu1542 Atlantis (线段树+扫描线+离散化)
    HTML杂记
    OpenCV中图像算术操作与逻辑操作
    java集群优化——ORM框架查询优化原理
    RVM切换ruby版本号
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4436386.html
Copyright © 2011-2022 走看看