原文:

给定n行和m个列的 2d 数组矩阵。 如图所示,以 zig-zag 方式打印此矩阵。

示例

input: 
1 2 3
4 5 6
7 8 9
output: 
1 2 4 7 5 3 6 8 9

c 代码的方法:这种方法很简单。 只需一次遍历每个对角线元素,然后根据上一个匹配项更改方向即可。

python3 代码的方法:这种方法很简单。 在以通常的方式行进矩阵时,如果元素ij的总和分别为偶数或奇数,则根据元素索引总和的奇偶校验,将该特定元素添加到列表的开头或末尾 。 按原样打印pg电子试玩链接的解决方案列表。

c

/* c   program to print matrix in zig-zag pattern*/
#include  
using namespace std; 
#define c 3 
// utility function to print matrix 
// in zig-zag form 
void zigzagmatrix(int arr[][c], int n, int m) 
{ 
    int row = 0, col = 0; 
    // boolean variable that will true if we 
    // need to increment 'row' value otherwise 
    // false- if increment 'col' value 
    bool row_inc = 0; 
    // print matrix of lower half zig-zag pattern 
    int mn = min(m, n); 
    for (int len = 1; len <= mn;   len) { 
        for (int i = 0; i < len;   i) { 
            cout << arr[row][col] << " "; 
            if (i   1 == len) 
                break; 
            // if row_increment value is true 
            // increment row and decrement col 
            // else decrement row and increment 
            // col 
            if (row_inc) 
                  row, --col; 
            else
                --row,   col; 
        } 
        if (len == mn) 
            break; 
        // update row or col value according 
        // to the last increment 
        if (row_inc) 
              row, row_inc = false; 
        else
              col, row_inc = true; 
    } 
    // update the indexes of row and col variable 
    if (row == 0) { 
        if (col == m - 1) 
              row; 
        else
              col; 
        row_inc = 1; 
    } 
    else { 
        if (row == n - 1) 
              col; 
        else
              row; 
        row_inc = 0; 
    } 
    // print the next half zig-zag pattern 
    int max = max(m, n) - 1; 
    for (int len, diag = max; diag > 0; --diag) { 
        if (diag > mn) 
            len = mn; 
        else
            len = diag; 
        for (int i = 0; i < len;   i) { 
            cout << arr[row][col] << " "; 
            if (i   1 == len) 
                break; 
            // update row or col value according 
            // to the last increment 
            if (row_inc) 
                  row, --col; 
            else
                  col, --row; 
        } 
        // update the indexes of row and col variable 
        if (row == 0 || col == m - 1) { 
            if (col == m - 1) 
                  row; 
            else
                  col; 
            row_inc = true; 
        } 
        else if (col == 0 || row == n - 1) { 
            if (row == n - 1) 
                  col; 
            else
                  row; 
            row_inc = false; 
        } 
    } 
} 
// driver code 
int main() 
{ 
    int matrix[][3] = { { 1, 2, 3 }, 
                        { 4, 5, 6 }, 
                        { 7, 8, 9 } }; 
    zigzagmatrix(matrix, 3, 3); 
    return 0; 
} 

java

/* java program to print matrix in zig-zag pattern*/
class gfg { 
  static final int c = 3; 
  // utility function to print matrix 
  // in zig-zag form 
  static void zigzagmatrix(int arr[][], int n, int m) { 
    int row = 0, col = 0; 
    // boolean variable that will true if we 
    // need to increment 'row' value otherwise 
    // false- if increment 'col' value 
    boolean row_inc = false; 
    // print matrix of lower half zig-zag pattern 
    int mn = math.min(m, n); 
    for (int len = 1; len <= mn;   len) { 
      for (int i = 0; i < len;   i) { 
        system.out.print(arr[row][col]   " "); 
        if (i   1 == len) 
          break; 
        // if row_increment value is true 
        // increment row and decrement col 
        // else decrement row and increment 
        // col 
        if (row_inc) { 
            row; 
          --col; 
        } else { 
          --row; 
            col; 
        } 
      } 
      if (len == mn) 
        break; 
      // update row or col valaue according 
      // to the last increment 
      if (row_inc) { 
          row; 
        row_inc = false; 
      } else { 
          col; 
        row_inc = true; 
      } 
    } 
    // update the indexes of row and col variable 
    if (row == 0) { 
      if (col == m - 1) 
          row; 
      else
          col; 
      row_inc = true; 
    } else { 
      if (row == n - 1) 
          col; 
      else
          row; 
      row_inc = false; 
    } 
    // print the next half zig-zag pattern 
    int max = math.max(m, n) - 1; 
    for (int len, diag = max; diag > 0; --diag) { 
      if (diag > mn) 
        len = mn; 
      else
        len = diag; 
      for (int i = 0; i < len;   i) { 
        system.out.print(arr[row][col]   " "); 
        if (i   1 == len) 
          break; 
        // update row or col value according 
        // to the last increment 
        if (row_inc) { 
            row; 
          --col; 
        } else { 
            col; 
          --row; 
        } 
      } 
      // update the indexes of row and col variable 
      if (row == 0 || col == m - 1) { 
        if (col == m - 1) 
            row; 
        else
            col; 
        row_inc = true; 
      } 
      else if (col == 0 || row == n - 1) { 
        if (row == n - 1) 
            col; 
        else
            row; 
        row_inc = false; 
      } 
    } 
  } 
  // driver code 
  public static void main(string[] args) { 
    int matrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 
    zigzagmatrix(matrix, 3, 3); 
  } 
} 
// this code is contributed by anant agarwal. 

python3

# program to print matrix in zig-zag pattern 
matrix =[ 
            [ 1, 2, 3,], 
            [ 4, 5, 6 ], 
            [ 7, 8, 9 ], 
        ] 
rows=3
columns=3
solution=[[] for i in range(rows columns-1)] 
for i in range(rows): 
    for j in range(columns): 
        sum=i j 
        if(sum%2 ==0): 
            #add at beginning 
            solution[sum].insert(0,matrix[i][j]) 
        else: 
            #add at end of the list 
            solution[sum].append(matrix[i][j]) 
# print the solution as it as 
for i in solution: 
    for j in i: 
        print(j,end=" ") 

c#

// c# program to print matrix 
// in zig-zag pattern 
using system; 
class gfg { 
    static int c = 3; 
    // utility function to print 
    // matrix in zig-zag form 
    static void zigzagmatrix(int[, ] arr, int n, int m) 
    { 
        int row = 0, col = 0; 
        // boolean variable that will 
        // true if we need to increment 
        // 'row' valueotherwise false- 
        // if increment 'col' value 
        bool row_inc = false; 
        // print matrix of lower half 
        // zig-zag pattern 
        int mn = math.min(m, n); 
        for (int len = 1; len <= mn;   len) { 
                for (int i = 0; i < len;   i) { 
                console.write(arr[row, col]   " "); 
                if (i   1 == len) 
                    break; 
                // if row_increment value is true 
                // increment row and decrement col 
                // else decrement row and increment 
                // col 
                if (row_inc) { 
                      row; 
                    --col; 
                } 
                else { 
                    --row; 
                      col; 
                } 
            } 
            if (len == mn) 
                break; 
            // update row or col valaue 
            // according to the last 
            // increment 
            if (row_inc) { 
                      row; 
                    row_inc = false; 
            } 
            else { 
                  col; 
                row_inc = true; 
            } 
        } 
        // update the indexes of row 
        // and col variable 
        if (row == 0) { 
            if (col == m - 1) 
                  row; 
            else
                  col; 
            row_inc = true; 
        } 
        else { 
            if (row == n - 1) 
                  col; 
            else
                  row; 
            row_inc = false; 
        } 
        // print the next half 
        // zig-zag pattern 
        int max = math.max(m, n) - 1; 
        for (int len, diag = max; diag > 0; --diag) { 
            if (diag > mn) 
                    len = mn; 
            else
                len = diag; 
            for (int i = 0; i < len;   i) { 
                    console.write(arr[row, col]   " "); 
                if (i   1 == len) 
                    break; 
                // update row or col value 
                // according to the last 
                // increment 
                if (row_inc) { 
                          row; 
                        --col; 
                } 
                else { 
                      col; 
                    --row; 
                } 
            } 
            // update the indexes of 
            // row and col variable 
            if (row == 0 || col == m - 1) { 
                if (col == m - 1) 
                      row; 
                else
                      col; 
                row_inc = true; 
            } 
            else if (col == 0 || row == n - 1) { 
                if (row == n - 1) 
                      col; 
                else
                      row; 
                row_inc = false; 
            } 
        } 
    } 
    // driver code 
    public static void main() 
    { 
        int[, ] matrix = { { 1, 2, 3 }, 
                        { 4, 5, 6 }, 
                        { 7, 8, 9 } }; 
        zigzagmatrix(matrix, 3, 3); 
    } 
} 
// this code is contributed by vt_m. 

php

 0; --$diag)  
    { 
        if ($diag > $mn) 
            $len = $mn; 
        else
            $len = $diag; 
        for ($i = 0;  
             $i < $len;   $i) 
        { 
            echo($arr[$row][$col] . " "); 
            if ($i   1 == $len) 
                break; 
            // update row or col  
            // value according to 
            // the last increment 
            if ($row_inc) 
            { 
                  $row; --$col; 
            } 
            else
            { 
                  $col; --$row; 
            } 
        } 
        // update the indexes of 
        // row and col variable 
        if ($row == 0 ||  
            $col == $m - 1)  
        { 
            if ($col == $m - 1) 
                  $row; 
            else
                  $col; 
            $row_inc = true; 
        } 
        else if ($col == 0 ||  
                 $row == $n - 1)  
        { 
            if ($row == $n - 1) 
                  $col; 
            else
                  $row; 
            $row_inc = false; 
        } 
    } 
} 
// driver code 
$matrix = array(array(1, 2, 3), 
                array(4, 5, 6), 
                array(7, 8, 9)); 
zigzagmatrix($matrix, 3, 3); 
// this code is contributed by  
// manish shaw(manishshaw1) 
?> 

输出

1 2 4 7 5 3 6 8 9 

时间复杂度o(n * m)

辅助空间o(1)