Diagonal traverse
Given a matrix (M rows, N columns) contains M x N elements, all the elements return to the matrix diagonal traversal order, diagonal traverse as shown below.
. 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:
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,4,7,5,3,6,8,9]
Explanation:
Description:
- The total number of elements in a given matrix to no more than 100,000.
Ideas :
Examples of the range of two-dimensional array are input 0-2
First look traversal rules: (0,0) -> (0,1) -> (1,0) -> (2,0) -> (1,1) -> (0,2) -> ( 2) -> (2,1) -> (2,2)
Array index (m, n), two modifications embodiment 1, (m-1, n + 1) 2, (m + 1, n-1)
From the array (0,0) starts, first (m-1, n + 1), (0,0) -> (- 1,1) case m = -1, out of range, m 0 is assigned. Then switch to change the way the index (m + 1, n-1), performing two (0,1) -> (1,0) -> (2, -1), n 0 is assigned to obtain (2,0), again changing the switching mode index (m-1, n + 1) is out of range until the next (2,0) -> (1,1) -> (0,2) -> (- 1,3). At this point m <0 and n> 2 are out of range, (m + 2, n-1), n is determined whether the priority should be out of range, the implementation of (m + 2, n-1) -> (1,2), to avoid since m <0 once again switched to change the way the index. Then the normal handover: (1,2) -> (2,1) -> (3,0), because m> 2, and the switching mode (m-1, n + 2)
java:
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix.length==0||matrix[0].length==0)return new int[0];
int col=matrix.length,row=matrix[0].length;
int nums=col*row,m=0,n=0;
int res[]=new int[nums];
boolean flag=true;
for(int i=0;i<nums;i++){
res[i]=matrix[m][n];
if(flag){
n+=1; m-=1;
}else{
n-=1; m+=1;
}
if(m>=col){
m-=1; n+=2; flag=true;
}else if(n>=row){
n-=1; m+=2; flag=false;
}
if(m<0){
m=0; flag=false;
}else if(n<0){
n=0; flag=true;
}
}
return res;
}
}
important point:
if (matrix.length==0||matrix[0].length==0)return new int[0];
Is first determined whether an empty array, additional matrix.length==0||matrix[0].length==0
sequence determination condition can not be reversed, because if matrix.length==0
the latter matrix[0].length==0
will not be determined, i.e., returns an empty array; however, matrix[0].length==0
when the front, if the input array is empty, matrix[0]
will be given because no matrix index 0.
It is determined in the first loop for m, if n is greater than or equal to the respective maximum length, then execute (m-1, n + 2), (m + 2, n-1). Avoid m, n Boolean flag while less than twice the conversion error 0:00.
python:
class Solution:
def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
if(len(matrix)==0 or len(matrix[0])==0):
return []
col=len(matrix)
row=len(matrix[0])
nums=col*row
m=n=0
flag=True
res=[]
for i in range(nums):
res.append(matrix[m][n])
if flag:
m-=1
n+=1
else:
m+=1
n-=1
if m>=col:
m-=1
n+=2
flag=True
elif n>=row:
m+=2
n-=1
flag=False
if m<0:
m=0
flag=False
elif n<0:
n=0
flag=True
return res