二维数组右上左下python
时间: 2023-12-14 15:35:07 浏览: 170
Python中创建二维数组
根据提供的引用[1],以下是Python实现二维数组的右上到左下遍历的代码和结果展示:
```python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m, n = len(matrix), len(matrix[0])
res = []
for k in range(m + n - 1):
tmp = []
i, j = min(k, m - 1), max(0, k - m + 1)
while i >= 0 and j < n:
tmp.append(matrix[i][j])
i -= 1
j += 1
res.append(tmp)
print(res)
```
输出结果为:
```
[[1], [4, 2], [7, 5, 3], [8, 6], [9]]
```
根据提供的引用,以下是Python实现判断二维数组中是否含有指定整数的代码:
```python
def findNumberIn2DArray(matrix, target):
if not matrix:
return False
m, n = len(matrix), len(matrix[0])
i, j = 0, n - 1
while i < m and j >= 0:
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
j -= 1
else:
i += 1
return False
```
阅读全文