首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
对角线遍历矩阵
[编程题]对角线遍历矩阵
热度指数:1414
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个大小为 n*m 的矩阵,请以对角线遍历并返回遍历结果
数据范围:
,矩阵中的元素满足
示例1
输入
[[1,2,3],[4,5,6],[7,8,9]]
输出
[1,2,4,7,5,3,6,8,9]
示例2
输入
[[1,3],[2,4]]
输出
[1,3,2,4]
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(47)
分享
纠错
提交结果有问题?
9个回答
10篇题解
开通博客
AimerAimer
发表于 2022-03-07 19:20:44
题意: 给定一个大小为 n*m 的矩阵,请以对角线遍历并返回遍历结果。 方法一: 模拟 思路: &nbs
展开全文
代码界的小白
发表于 2022-03-11 00:04:44
题目主要信息 给定一个大小为 n*m 的矩阵,请以对角线遍历并返回遍历结果 方法一:翻转输出 具体方法 新建一个数组result,存储最终的结果 使用一个循环遍历所有的对角线,第一行和最后一列的元素都是对角线的起点 使用一个内层循环对所有对角线上的所有元素,可以计算指定对角线上的元素数量 由于不知
展开全文
Mr.galaxy
发表于 2023-03-15 21:40:07
#include <cstddef> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @
展开全文
尤泰燃
发表于 2022-06-29 13:42:48
public int[] diagonalOrder (int[][] mat) { // write code here int i=0; int j=0; int n = mat.length; int m = ma
展开全文
牛客362459853号
发表于 2021-11-24 00:23:53
描述 给定一个大小为 n*m 的矩阵,请以对角线遍历并返回遍历结果 数据范围: ,矩阵中的元素满足 例1、输入:[[1,2,3],[4,5,6],[7,8,9]],输出: [1,2,4,7,5,3,6,8,9] 例2
展开全文
辣椒酱up
发表于 2024-03-17 14:44:45
class Solution { public: vector<int> diagonalOrder(vector<vector<int>>& mat) { vector<int> res;
展开全文
卢然小子
发表于 2022-07-24 22:39:34
思路: 我们可以先手动画一下,发现Z形走法在遇到边界的时候有2个方向——从右上往坐下走,此时x++,y--;从坐下往右上走,此时x--,y++。因此我们可以定义2个方向{dx=1,dy=-1}和{dx=-1,dy=1}。 另外,在走某一条路的时候,x+y的和是不变的,只有在(x,y)所代表的坐标到
展开全文
不会做题的小菜鸡
发表于 2022-03-02 11:52:08
题目分析 题目给出我们一个大小为n*m的二维数组矩阵 题目要求我们按照对角线的访问顺序访问二维数组,并且返回访问顺序的列表 方法一:分类讨论 实现思路 由于对角线访问的时候存在两个方向,因此我们记录一个方向转换的标记,通过奇偶来确定当前应该遍历的方向,偶数代表从左下遍历到右上的方向,奇数
展开全文
姐姐的遮阳伞
发表于 2022-03-31 14:51:03
import java.util.*; public class Solution { // 自定义一个类,表示的是每个点的坐标以及该点上的值 public class Coordinates { public int x; // x 坐标
展开全文
knuke
发表于 2022-11-25 19:29:39
## 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可## # @param mat int整型二维数组 # @return int整型一维数组#class Solution: 
展开全文
问题信息
数组
模拟
难度:
9条回答
47收藏
2980浏览
热门推荐
通过挑战的用户
查看代码
Asphyxiay
2022-09-14 01:58:47
牛客89818...
2022-09-13 17:02:07
牛客40225...
2022-09-12 12:43:04
三秋秋
2022-09-12 01:43:53
在写周报的放鸽...
2022-09-10 21:05:26
相关试题
一个文件里有10万个随机正整数,按...
去哪儿
堆
模拟
评论
(4)
一个10*10的矩阵(可以理解为棋...
去哪儿
模拟
评论
(0)
有两个文件context.txt和...
去哪儿
模拟
评论
(4)
Mysql中表student_in...
数据库
SQL
评论
(1)
下列表达式的值为True的是( )
Python
评论
(1)
对角线遍历矩阵
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ public int[] diagonalOrder (int[][] mat) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型vector
> * @return int整型vector */ vector
diagonalOrder(vector
>& mat) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @return int整型一维数组 # class Solution: def diagonalOrder(self , mat ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ public List
diagonalOrder (List
> mat) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ function diagonalOrder( mat ) { // write code here } module.exports = { diagonalOrder : diagonalOrder };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @return int整型一维数组 # class Solution: def diagonalOrder(self , mat: List[List[int]]) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ func diagonalOrder( mat [][]int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param matRowLen int mat数组行数 * @param matColLen int* mat数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* diagonalOrder(int** mat, int matRowLen, int* matColLen, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param mat int整型二维数组 # @return int整型一维数组 # class Solution def diagonalOrder(mat) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ def diagonalOrder(mat: Array[Array[Int]]): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ fun diagonalOrder(mat: Array
): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ public int[] diagonalOrder (int[][] mat) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ export function diagonalOrder(mat: number[][]): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ func diagonalOrder ( _ mat: [[Int]]) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @return int整型一维数组 */ pub fn diagonalOrder(&self, mat: Vec
>) -> Vec
{ // write code here } }
[[1,2,3],[4,5,6],[7,8,9]]
[1,2,4,7,5,3,6,8,9]
[[1,3],[2,4]]
[1,3,2,4]