前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >包子小道消息@2019tech掌门人变迁小结 & Leetcode 59 solution: Spiral Matrix II

包子小道消息@2019tech掌门人变迁小结 & Leetcode 59 solution: Spiral Matrix II

作者头像
包子面试培训
发布2020-01-02 11:51:58
3820
发布2020-01-02 11:51:58
举报

包子小道消息@12/28/2019

有人的地方就有江湖,2019年北美tech industry的掌门人也是几家欢喜几家愁,有人上位自然就有人离开,铁打的硬盘流水的兵。However, 这些帮主们早已经家财万贯,只是为了自己在wiki上面的履历再添一笔罢了,我等吃瓜群众看看热闹,默默YY一下如果有一天是我,喂,醒醒啦(#`O′)!

Out

  • Jon Ive@Apple, 不care了已经,无所谓,你们再怎么说也还是有人用苹果
  • Larry Page@Alphabet, 更不care了,太有钱了,去世之前赶紧花吧
  • Steve Burke@NBCU, streaming 确实弄不过disney和netflix,退休吧
  • Adam Neumann@WeWork, 应该可以和OFO肩并肩写进MBA教程的失败惨案,套现走人,这个人在圈里应该是废了,本来就是混子,赚了一笔走了也不会回来了
  • Angela Ahrendts@Apple, SVP, 背锅走吧,苹果卖不动了,尤其在中国有华为小米的情况下
  • Chris Cox@Facebook, CPO, 和小扎意见不合,bye bye

Promotions

  • Sundar Pichai@Alphabet, 一统江湖了,硅谷是印度人的谷真真更加实锤了
  • Alex Zhu@TikTok, musically的founder,目前主要负责TikTok,字节想让其脱离与抖音的关系,让世界都TikTok起来,实至名归!
  • Thomas Kurian@Google Cloud, 注意,虽然名字是托马斯,哥们也是个土生土长的印度兄弟(再次实锤),在Oracle搞了22年去了Google Cloud,更是让Larry Ellison老爷子气的70多岁又重出江湖负责OCI (Oracle Cloud Infrastructure)
  • John Stankey@AT&T,典型传统职业经理人(stereotype: 光头高个白人男,偶尔搭配一副银边眼镜),33年在AT&T磨一剑, 多年的媳妇儿终于熬成婆
  • Fidji Simo@Facebook, Chris Cox 一走顶上去了,女强人, 除了Sheryl,第二个report给小扎的femail高管
  • Brett Taylor@Salesforce, 在COO的岗位上干几年就是CEO了,只要不闹出幺蛾子,稳稳的

YY了这么多消息,然而并没有卵用,赶紧埋头刷题吧少年们??。大家都在过假期,你在刷题,你不成功谁成功!你不牛逼谁牛逼!

我们遇到什么困难,也不要怕!微笑着面对它,消除恐惧的最好办法就是面对恐惧。坚持,才是胜利。加油,奥利给!

Baozi Training Leetcode 59 solution: Spiral Matrix II

Blogger: https://blog.baozitraining.org/2019/12/leetcode-solution-59-sprial-matrix-ii.html

Youtube: https://youtu.be/5NpvVznQlFA

博客园: https://www.cnblogs.com/baozitraining/p/12016806.html

B站: https://www.bilibili.com/video/av78743578/

Problem Statement

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Example:

Input: 3
Output:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

Problem link

Video Tutorial

You can find the detailed video tutorial here

  • Youtube
  • B站

Thought Process

Straight forward question, could use Spiral Matrix exact same recursion algorithm to solve this, you can also watch that video tutorial here

Still needs pay attribute to this guy, had another great solution:

https://leetcode.com/problems/spiral-matrix-ii/discuss/22282/4-9-lines-Python-solutions

Solutions

 1 public int[][] generateMatrix(int n) {
 2     assert n >= 0;
 3 
 4     int[][] res = new int[n][n];
 5 
 6     this.generateMatrixHelper(0, 0, n, n, res, 1);
 7     return res;
 8 }
 9 
10 private void generateMatrixHelper(
11         int i,
12         int j,
13         int rowSize,
14         int colSize,
15         int[][] matrix,
16         int num
17 ) {
18     if (rowSize <= 0 || colSize <= 0) {
19         return;
20     }
21 
22     if (rowSize == 1 && colSize == 1) {
23         matrix[i][j] = num;
24         return;
25     }
26     if (rowSize == 1) {
27         for (int k = j; k < j + colSize; k++) {
28             matrix[i][k] = num;
29             num++;
30         }
31         return;
32     }
33 
34     if (colSize == 1) {
35         for (int k = i; k < i + rowSize; k++) {
36             matrix[k][j] = num;
37             num++;
38         }
39         return;
40     }
41 
42     // do the spiral
43     for (int k = j; k < j + colSize; k++) {
44         matrix[i][k] = num++;
45     }
46 
47     for (int k = i + 1; k < i + rowSize; k++) {
48         matrix[k][j + colSize - 1] = num++;
49     }
50 
51     for (int k = j + colSize - 2; k >= i; k--) {
52         matrix[i + rowSize - 1][k] = num++;
53     }
54 
55     for (int k = i + rowSize - 2; k > i; k--) {   // both the start and end need to be i, j, and also care about length
56         matrix[k][j] = num++;
57     }
58 
59     this.generateMatrixHelper(i + 1, j + 1, rowSize - 2, colSize - 2, matrix, num);
60 }
Simulation using Recursion

Time Complexity: O(M*N) where M, N is row and col of matrix

Space Complexity: O(M*N) since we used list to store the result, where M, N is row and col of matrix

References

  • Spiral Matrix
  • The Information
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 包子铺里聊IT 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Statement
  • Video Tutorial
  • Thought Process
  • Solutions
    • Simulation using Recursion
    • References
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档