498. Diagonal Traverse对角线z型traverse
[抄题]:
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Explanation:

[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么控制方向:由于每次只走一格,所以用xy+-d即可,d=1
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 为了保证第一个数一样,都是先直接添加后再处理
result[i] = matrix[row][col];
row -= d;
col += d;
[二刷]:
- 为了避免处理>边界时顺便把<边界处理了,<边界的小情况要写在后面
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
换方向用d = -d来控制
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
public class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0) return new int[0];
int m = matrix.length, n = matrix[0].length;
int[] result = new int[m * n];
int row = 0, col = 0, d = 1;
//for loop: add to result, expand, handle corner cases
for (int i = 0; i < m * n; i++) {
result[i] = matrix[row][col];
row -= d;
col += d;
if (row >= m) {row = m - 1; col += 2; d = -d;}
if (col >= n) {col = n - 1; row += 2; d = -d;}
if (row < 0) {row = 0; d = -d;}
if (col < 0) {col = 0; d = -d;}
}
return result;
}
}
498. Diagonal Traverse对角线z型traverse的更多相关文章
- 【LeetCode】498. Diagonal Traverse 解题报告(Python)
[LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...
- 498 Diagonal Traverse 对角线遍历
详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...
- [LeetCode] Diagonal Traverse 对角线遍历
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- LeetCode - 498. Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- 498. Diagonal Traverse
题目思路 题目来源 C++实现 class Solution { public: vector<int> findDiagonalOrder(vector<vector<int ...
- CCF真题Z型输出
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...
- 字符串的z型转换
class Solution(object): def convert(self, s, numRows): if numRows==1: return ...
- 算法:Z字型(Zigzag)编排
问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...
- matplotlib柱状图、面积图、直方图、散点图、极坐标图、箱型图
一.柱状图 1.通过obj.plot() 柱状图用bar表示,可通过obj.plot(kind='bar')或者obj.plot.bar()生成:在柱状图中添加参数stacked=True,会形成堆叠 ...
随机推荐
- 【java多线程】队列系统之说说队列Queue
转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...
- CSV表格融合
常用表格融合函数 1 merge() 用于融合的函数 https://blog.csdn.net/brucewong0516/article/details/82707492 pd.merge(lef ...
- react hooks 笔记
1. 建议安装以上版本: "dependencies": { "react": "^16.7.0-alpha.2", "react ...
- Hanlp自然语言处理中的词典格式说明
使用过hanlp的都知道hanlp中有许多词典,它们的格式都是非常相似的,形式都是文本文档,随时可以修改.本篇文章详细介绍了hanlp中的词典格式,以满足用户自定义的需要. 基本格式 词典分为词频词性 ...
- MySQL查询不使用索引汇总 + 如何优化sql语句
不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...
- 黄聪:C#使用Application.Restart重启程序出错解决办法
调用 Application.Restart重启程序出错 解决办法,就是给程序的.exe文件,加上下面的设置
- mysql 将行拼接成字符串的方法
见代码: ;//保证可以拼接足够长的字符串,没它 数据量大时会截断结果1 group by videoType 效果如下:
- PUSU 拆分后发货和开票的时间节点问题
项目做到现在业务突然说流程要变,心中顿时无数个草草草掠过.这公司业务也真是够奇葩了,一天一个样.原来流程是由PU把产品生产完后就发给SU,由SU再来决定什么时候对客户和开票.而现在马上要上线了,突然冒 ...
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》
https://blog.csdn.net/c406495762/article/details/60137956 运行平台:Windows Python版本:Python3.x IDE:Sublim ...
- 教你phpstudy如何搭建本地多站点
经常做多个网站同时开发,如何才能在本地能使部署多个站点,今天就来分享一下如何用PHPstudy搭建本地多站点. 点击上图中的 其它选项菜单 ,就会弹出下面的对话框,然后点击 站点域名管理 然后在 网站 ...