首页 > 其他分享 >leetcode-566-easy

leetcode-566-easy

时间:2022-11-03 08:00:12浏览次数:45  
标签:matrix int 566 ++ length result easy leetcode mat

Reshape the Matrix

In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.

You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

 

Example 1:


Input: mat = [[1,2],[3,4]], r = 1, c = 4
Output: [[1,2,3,4]]
Example 2:


Input: mat = [[1,2],[3,4]], r = 2, c = 4
Output: [[1,2],[3,4]]

思路一: 想到的思路是把数字全部收集起来,然后在新数组中重新赋值。看了题解发现赋值有效率更高的方法。假设 n 为原数组的列,c 为新数组的列,那么有结论第 x 个元素在原数组中的下标为(x/n, x%n),而在新数组的对应下标为(x/c, x%c)

public int[][] matrixReshape(int[][] mat, int r, int c) {
	int size = r * c;
	if (size != mat.length * mat[0].length) return mat;

	int[][] result = new int[r][c];

	List<Integer> values = new ArrayList<>();
	for (int i = 0, matLength = mat.length; i < matLength; i++) {
		int[] ints = mat[i];
		for (int j = 0; j < mat[0].length; j++) {
			values.add(ints[j]);
		}
	}

	int idx = 0;
	for (int i = 0; i < result.length; i++) {
		for (int j = 0; j < result[0].length; j++) {
			result[i][j] = values.get(idx++);
		}
	}

	return result;
}

标签:matrix,int,566,++,length,result,easy,leetcode,mat
From: https://www.cnblogs.com/iyiluo/p/16853158.html

相关文章

  • LeetCode刷题记录.Day4
    移除链表元素题目链接203.移除链表元素-力扣(LeetCode)classSolution{public:ListNode*removeElements(ListNode*head,intval){ListNode*varHe......
  • leetcode-104. 二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,......
  • 【leetcode 952. 按公因数计算最大组件大小】【欧拉筛+并查集】
    importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;classSolution{List<Integer>list=newArrayList<>();intprimeNum=0......
  • Oeasyvim - 如打怪一般的学习
    Oeasyvim-如打怪一般的学习项目地址:https://github.com/TonyK922/oeasy-vim-tutorial这是overmind1980做的一个vim入手到进阶的教程很适合vim初学者.项目共98章......
  • leetcode-67. 二进制求和
    题目描述给你两个二进制字符串a和b,以二进制字符串的形式返回它们的和。示例输入:a="11",b="1"输出:"100"思路分析我们可以先将其转化为整数,相加之后再转为......
  • leetcode股票系列问题
    本文整合了一些大佬的文章加上自己的一些认识,供自己复习转载:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solutions/8753/yi-ge-fang-fa-tuan-mie-6-d......
  • AI智能检测识别EasyCVR视频融合平台告警抓拍图片的逻辑优化
    将智能分析网关的AI智能识别能力与EasyCVR视频融合平台的视频服务能力融合,构建基于云边端协同架构的安全风险监测与视频监管平台,可对接入的多路视频流进行智能检测、智能识......
  • EasyCVR平台https协议用不了rtc,WebRTC视频无法播放该如何解决?
    EasyCVR平台可支持多类型设备、多协议方式接入,具体包括:国标GB28181协议、RTMP、RTSP/Onvif、海康Ehome,以及海康SDK、大华SDK、华为SDK、宇视SDK、乐橙SDK、萤石SDK等,可覆盖......
  • LeetCode刷题第一周
    数组:内存空间连续,数据类型统一,下标从0开始二分查找704classSolution{publicintsearch(int[]nums,inttarget){//方法一:暴力解法//for......
  • AI智能检测识别EasyCVR视频融合平台告警抓拍图片的逻辑优化
    将智能分析网关的AI智能识别能力与EasyCVR视频融合平台的视频服务能力融合,构建基于云边端协同架构的安全风险监测与视频监管平台,可对接入的多路视频流进行智能检测、智能识......