avatar
v*o
1
Given a 2D matrix, print all elements of the given matrix in diagonal order.
For example, consider the following 5 X 4 input matrix.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Diagonal printing of the above matrix is
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
avatar
d*x
2
今天貌似有一次震感比较强的。。。
avatar
p*g
3
avatar
t*y
4
void printDiagonal(const vector>& m){
assert(!m.empty() && !m[0].empty());
int rows = m.size();
int cols = m[0].size();
for(int i=0; iint j=i;
for(int k=0; j>=0 && k < cols;++k,--j){
cout << m[j][k] << " ";
}
cout << endl;
}
}
avatar
s*d
5
赞后知后觉!

【在 d**********x 的大作中提到】
: 今天貌似有一次震感比较强的。。。
avatar
h*e
6
楼上你的逻辑在 i >= rows时候也对么

【在 t*******y 的大作中提到】
: void printDiagonal(const vector>& m){
: assert(!m.empty() && !m[0].empty());
: int rows = m.size();
: int cols = m[0].size();
: for(int i=0; i: int j=i;
: for(int k=0; j>=0 && k < cols;++k,--j){
: cout << m[j][k] << " ";
: }
: cout << endl;

avatar
m*r
7
排队赞!
avatar
h*g
8
int m=matrix.size();
int n=matrix[0].size();
int i=0;
while(iint j=0;
int a=i;
do{
cout<} while(a>=0&&ji++;
cout<}
int j=1;
while(jint i=m-1;
int b=j;
do{
cout<} while(i>=0&&bj++;
cout<}
avatar
t*y
9
有bug,对最后一行特殊处理。

【在 h*******e 的大作中提到】
: 楼上你的逻辑在 i >= rows时候也对么
avatar
h*e
10
恩,就是k 和 j 的初值在最后一行第2个到最后的时候处理一下就好了,其他的写
得都还挺好。

【在 t*******y 的大作中提到】
: 有bug,对最后一行特殊处理。
avatar
t*y
11
谢谢大牛夸奖

【在 h*******e 的大作中提到】
: 恩,就是k 和 j 的初值在最后一行第2个到最后的时候处理一下就好了,其他的写
: 得都还挺好。

avatar
h*e
12
哪有,我不是大牛哈。呵呵,大家一起互相学习, 一起讨论 一起提高么。

【在 t*******y 的大作中提到】
: 谢谢大牛夸奖
avatar
x*e
13
g家的
avatar
v*o
14
void diagonalOrder(int matrix[ROW][COL])
{
// There will be ROW+COL-1 lines in the output
for (int line=1; line<=(ROW + COL -1); line++)
{
/* Get column index of the first element in this line of output.
The index is 0 for first ROW lines and line - ROW for remaining
lines */
int start_col = max(0, line-ROW);
/* Get count of elements in this line. The count of elements is
equal to minimum of line number, COL-start_col and ROW */
int count = min(line, (COL-start_col), ROW);
/* Print elements of this line */
for (int j=0; jprintf("%5d ", matrix[min(ROW, line)-j-1][start_col+j]);
/* Ptint elements of next diagonal on next line */
printf("n");
}
}

order.

【在 v***o 的大作中提到】
: Given a 2D matrix, print all elements of the given matrix in diagonal order.
: For example, consider the following 5 X 4 input matrix.
: 1 2 3 4
: 5 6 7 8
: 9 10 11 12
: 13 14 15 16
: 17 18 19 20
: Diagonal printing of the above matrix is
: 1
: 5 2

avatar
a*0
15
厉害,能不能讲讲您是怎么想到这个的,:-)
int count = min(line, (COL-start_col), ROW);
thanks

【在 v***o 的大作中提到】
: void diagonalOrder(int matrix[ROW][COL])
: {
: // There will be ROW+COL-1 lines in the output
: for (int line=1; line<=(ROW + COL -1); line++)
: {
: /* Get column index of the first element in this line of output.
: The index is 0 for first ROW lines and line - ROW for remaining
: lines */
: int start_col = max(0, line-ROW);
: /* Get count of elements in this line. The count of elements is

avatar
w*e
16
6x4的input应该排成啥样
1
2
3
4
4
4
3
2
1

7*4的input呢
avatar
w*h
18
public static void diagonalMatrix(int[][] M){
int row=M.length; int col=M[0].length;
int diag_row=row+col-1;//# rows for the new diagonal matrix
int diag_col=Math.min(col,row);
int new_row=0,new_i=0,new_j=0;
for(int i=0;ifor (int j=0;j<=diag_col;j++){
new_row=row-j;//
new_i=i-j;new_j=j;
if(new_i<0){System.out.printf("\n");break;}
if(new_i>new_row-1) {new_i=new_row-1;new_j=i-new_i;}
if(new_j>col-1||new_i<0){System.out.printf("\n");break;}
System.out.printf(String.valueOf(M[new_i][new_j])+" ");
}
}
avatar
a*e
19
这是一个很简洁的递归问题,为什么没有人给出这方面的解答?diag(X) 可以分解成:
diag(X) = combine(leftColumnOf(X), diag(RightColumnsOf(X)))
以下是用 Haskell 写的答案:
diag [] = []
diag ([]:xs) = [] : diag xs
diag a = [x] : comb l (diag r)
where
x:l = map head a
r = map tail a
comb [] s = s
comb x [] = [x]
comb (x:y) (s:r) = (x:s) : comb y r
avatar
m*s
20
void printDigMatrix(int **m, int r, int c){
for(int i=0; iint j;
if(i>=r){
j = i-r+1;
}
else j=0;
while(j<=i&&jprintf("%d ", m[i-j][j]);
j++;
}
printf("\n");
}
}
avatar
v*o
21
Given a 2D matrix, print all elements of the given matrix in diagonal order.
For example, consider the following 5 X 4 input matrix.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Diagonal printing of the above matrix is
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
avatar
t*y
22
void printDiagonal(const vector>& m){
assert(!m.empty() && !m[0].empty());
int rows = m.size();
int cols = m[0].size();
for(int i=0; iint j=i;
for(int k=0; j>=0 && k < cols;++k,--j){
cout << m[j][k] << " ";
}
cout << endl;
}
}
avatar
h*e
23
楼上你的逻辑在 i >= rows时候也对么

【在 t*******y 的大作中提到】
: void printDiagonal(const vector>& m){
: assert(!m.empty() && !m[0].empty());
: int rows = m.size();
: int cols = m[0].size();
: for(int i=0; i: int j=i;
: for(int k=0; j>=0 && k < cols;++k,--j){
: cout << m[j][k] << " ";
: }
: cout << endl;

avatar
h*g
24
int m=matrix.size();
int n=matrix[0].size();
int i=0;
while(iint j=0;
int a=i;
do{
cout<} while(a>=0&&ji++;
cout<}
int j=1;
while(jint i=m-1;
int b=j;
do{
cout<} while(i>=0&&bj++;
cout<}
avatar
t*y
25
有bug,对最后一行特殊处理。

【在 h*******e 的大作中提到】
: 楼上你的逻辑在 i >= rows时候也对么
avatar
h*e
26
恩,就是k 和 j 的初值在最后一行第2个到最后的时候处理一下就好了,其他的写
得都还挺好。

【在 t*******y 的大作中提到】
: 有bug,对最后一行特殊处理。
avatar
t*y
27
谢谢大牛夸奖

【在 h*******e 的大作中提到】
: 恩,就是k 和 j 的初值在最后一行第2个到最后的时候处理一下就好了,其他的写
: 得都还挺好。

avatar
h*e
28
哪有,我不是大牛哈。呵呵,大家一起互相学习, 一起讨论 一起提高么。

【在 t*******y 的大作中提到】
: 谢谢大牛夸奖
avatar
x*e
29
g家的
avatar
v*o
30
void diagonalOrder(int matrix[ROW][COL])
{
// There will be ROW+COL-1 lines in the output
for (int line=1; line<=(ROW + COL -1); line++)
{
/* Get column index of the first element in this line of output.
The index is 0 for first ROW lines and line - ROW for remaining
lines */
int start_col = max(0, line-ROW);
/* Get count of elements in this line. The count of elements is
equal to minimum of line number, COL-start_col and ROW */
int count = min(line, (COL-start_col), ROW);
/* Print elements of this line */
for (int j=0; jprintf("%5d ", matrix[min(ROW, line)-j-1][start_col+j]);
/* Ptint elements of next diagonal on next line */
printf("n");
}
}

order.

【在 v***o 的大作中提到】
: Given a 2D matrix, print all elements of the given matrix in diagonal order.
: For example, consider the following 5 X 4 input matrix.
: 1 2 3 4
: 5 6 7 8
: 9 10 11 12
: 13 14 15 16
: 17 18 19 20
: Diagonal printing of the above matrix is
: 1
: 5 2

avatar
a*0
31
厉害,能不能讲讲您是怎么想到这个的,:-)
int count = min(line, (COL-start_col), ROW);
thanks

【在 v***o 的大作中提到】
: void diagonalOrder(int matrix[ROW][COL])
: {
: // There will be ROW+COL-1 lines in the output
: for (int line=1; line<=(ROW + COL -1); line++)
: {
: /* Get column index of the first element in this line of output.
: The index is 0 for first ROW lines and line - ROW for remaining
: lines */
: int start_col = max(0, line-ROW);
: /* Get count of elements in this line. The count of elements is

avatar
w*e
32
6x4的input应该排成啥样
1
2
3
4
4
4
3
2
1

7*4的input呢
avatar
w*h
34
public static void diagonalMatrix(int[][] M){
int row=M.length; int col=M[0].length;
int diag_row=row+col-1;//# rows for the new diagonal matrix
int diag_col=Math.min(col,row);
int new_row=0,new_i=0,new_j=0;
for(int i=0;ifor (int j=0;j<=diag_col;j++){
new_row=row-j;//
new_i=i-j;new_j=j;
if(new_i<0){System.out.printf("\n");break;}
if(new_i>new_row-1) {new_i=new_row-1;new_j=i-new_i;}
if(new_j>col-1||new_i<0){System.out.printf("\n");break;}
System.out.printf(String.valueOf(M[new_i][new_j])+" ");
}
}
avatar
a*e
35
这是一个很简洁的递归问题,为什么没有人给出这方面的解答?diag(X) 可以分解成:
diag(X) = combine(leftColumnOf(X), diag(RightColumnsOf(X)))
以下是用 Haskell 写的答案:
diag [] = []
diag ([]:xs) = [] : diag xs
diag a = [x] : comb l (diag r)
where
x:l = map head a
r = map tail a
comb [] s = s
comb x [] = [x]
comb (x:y) (s:r) = (x:s) : comb y r
avatar
m*s
36
void printDigMatrix(int **m, int r, int c){
for(int i=0; iint j;
if(i>=r){
j = i-r+1;
}
else j=0;
while(j<=i&&jprintf("%d ", m[i-j][j]);
j++;
}
printf("\n");
}
}
avatar
f*t
37
for (int i = 0; i < row + col - 1; ++i) {
for (int x = min(i, row - 1), y = max(0, i - row + 1); 0 <= x && y -x, ++y) {
cout << matrix[x][y] << 't';
}
cout << endl;
}

order.

【在 v***o 的大作中提到】
: Given a 2D matrix, print all elements of the given matrix in diagonal order.
: For example, consider the following 5 X 4 input matrix.
: 1 2 3 4
: 5 6 7 8
: 9 10 11 12
: 13 14 15 16
: 17 18 19 20
: Diagonal printing of the above matrix is
: 1
: 5 2

avatar
z*e
38
int row = A.length;
if(row == 0) return;
int col = A[0].length;
for(int i = 0; i <= row + col - 2; i++){
for(int j = 0; j <= i; j++) {
if (i - j < row && j < col) System.out.print(A[i - j][j] + "
");
}
if( i != row + col - 2) System.out.println();
}
avatar
w*t
39
G电面还有个变体,要更复杂一些,输出成这个样子:
1 5 2 3 6 9 13 10 7 4 8 11 14 17 18 15 12 16 19 20
就是说每次的起点并不是在左侧或底部,而是一个连续的zigzag。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。