原文:
给定 n 行 m 列的 2d 矩阵。如图所示,以 zig-zig 方式打印该矩阵。
例:
input:
1 2 3
4 5 6
7 8 9
output:
1 2 4 7 5 3 6 8 9
c 代码的方法 方法很简单。只需一次迭代一个对角元素,并根据之前的匹配改变方向。 python 3 的方法代码 这个方法很简单。当以通常的方式在矩阵中行进时,基于元素的索引的和的奇偶性,如果 i 和 j 的和分别是偶数或奇数,则将该特定元素添加到列表的开头或结尾。按原样打印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 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 = 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.
python 3
# 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 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 = 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.
服务器端编程语言(professional hypertext preprocessor 的缩写)
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) t3】辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处