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 = 1;
 9         while(list.size() < row*col){
10            if(i >= 0 && i < row && j >= 0 && j < col){
11                // System.out.println(matrix[i][j]);
12                list.add(matrix[i][j]);
13                if(flag == 1){
14                    i = i-1;
15                    j = j+1;
16                }else{
17                    i = i+1;
18                    j = j-1;
19                }
20            }else if(flag == 1){
21                if(j < col){
22                    i = i+1;
23                }else{
24                    i = i+2;
25                    j = j-1;
26
27                }
28                flag = - flag;
29            }else if(flag == -1){
30                if(i >= row){
31                    j = j+2;
32                    i = i-1;
33                }else{
34                    j = j+1;
35                }
36                flag = -flag;
37            }
38         }
39         int[] res = new int[row*col];
40         for(int k = 0; k < res.length; k++){
41             res[k] = list.get(k);
42         }
43         return res;
44     }
45 }

原文地址:https://www.cnblogs.com/goPanama/p/9887146.html

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

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

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

【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

过中等难度题目.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 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

List::traverse遍历

声明: // 遍历 void traverse(void (*)(T&)); //遍历,依次实施visit操作(函数指针,只读或局部性修改) template <typename VST> //操作器 void traverse(VST&); //遍历,依次实施visit操作(函数对象,可全局性修改) 定义: template <typename T> void List<T>::traverse(void (*visit)(T&)) //利用函