LeetCode 498. Diagonal Traverse

原题链接在这里:https://leetcode.com/problems/diagonal-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:

Note:

The total number of elements of the given matrix will not exceed 10,000.

题解:

The result size should be m*n.

For each value, when r + c is even, index in matrix is moving right up. First check if it is hitting right, if yes, move down. Then check if it is hitting up, if yes, move right. Otherwise move right up.

When r + c is odd, index in matrix is moving left down.First check if it is hitting down, if yes, move right. Then check if it is hitting left, if yes, move down. Otherwise move left down.

Think this as when it hit the corner, which direction it would move first.

Time Complexity: O(m*n). m = matrix.length. n = matrix[0].length.

Space: O(1). regardless res.

AC Java:

 1 class Solution {
 2     public int[] findDiagonalOrder(int[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
 4             return new int[0];
 5         }
 6
 7         int m = matrix.length;
 8         int n = matrix[0].length;
 9         int [] res = new int[m*n];
10         int r = 0;
11         int c = 0;
12         for(int i = 0; i<m*n; i++){
13             res[i] = matrix[r][c];
14
15             if((r + c) % 2 == 0){
16                 if(c == n-1){
17                     r++;
18                 }else if(r == 0){
19                     c++;
20                 }else{
21                     r--;
22                     c++;
23                 }
24             }else{
25                 if(r == m - 1){
26                     c++;
27                 }else if(c == 0){
28                     r++;
29                 }else{
30                     r++;
31                     c--;
32                 }
33             }
34         }
35
36         return res;
37     }
38 }

类似Spiral Matrix.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12015933.html

时间: 2024-07-30 23:09:36

LeetCode 498. Diagonal Traverse的相关文章

498. Diagonal 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: Note: The total

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: [暴力解法]: 时间

498. Diagonal Traverse

1 class Solution { 2 public int[] findDiagonalOrder(int[][] matrix) { 3 if(matrix.length == 0) return new int[0]; 4 int row = matrix.length; 5 int col = matrix[0].length; 6 int i = 0, j = 0; 7 List<Integer> list = new ArrayList<>(); 8 int flag

【LeetCode】4.Array and String — Diagonal 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: Note: The total

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *