LEETCODE 498. 对角线遍历

https://leetcode-cn.com/problems/diagonal-traverse/

vector<int> nums;
int m = matrix.size();
int n = matrix[0].size();

int i = 0;	
while (i < m + n)
{	
	int x1 = (i < m) ? i : m - 1;	
	int y1 = i - x1;
	while (x1 >= 0 && y1 < n)
	{
		nums.push_back(matrix[x1][y1]);
		x1--;
		y1++;
	}
	i++;

	if (i >= m + n) break;
	int y2 = (i < n) ? i : n - 1;	
	int x2 = i - y2;
	while (y2 >= 0 && x2 < m)
	{
		nums.push_back(matrix[x2][y2]);
		x2++;
		y2--;
	}
	i++;
}
return nums;
posted @ 2020-03-20 21:09  aaaaassss  阅读(96)  评论(0编辑  收藏  举报