C语言刷“矩阵”类题目(2维矩阵/2级指针)
566. 重塑矩阵
int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes)
{
// 条件不满足,返回原矩阵
if (r * c != matSize * (*matColSize)) {
*returnSize = matSize;
*returnColumnSizes = malloc(sizeof(int) * matSize);
for (int i = 0; i < matSize; i++) {
(*returnColumnSizes)[i] = matColSize[i];
}
return mat;
} // 先把2维矩阵,转成1维
int *arrayOne = malloc(sizeof(int) * r * c);
for (int i = 0, count = 0; i < matSize; i++) {
for (int j = 0; j < *matColSize; j++) {
arrayOne[count++] = mat[i][j];
}
} // 再把1维转成需要的2维矩阵
// 方法:int**本质就是指向int*的指针,让它指向1维数组的不用部位即可
int **res = malloc(sizeof(int*) * r);
*returnSize = r;
*returnColumnSizes = malloc(sizeof(int) * r);
for (int i = 0; i < r; i++) {
(*returnColumnSizes)[i] = c;
// 开始换皮,把1维转成最终的2维
res[i] = arrayOne + i*c;
} return res;
}
54. 螺旋矩阵
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize)
{
if(matrixSize==0){
*returnSize = 0;
return NULL;
} int *res = malloc(sizeof(int) * matrixSize * (*matrixColSize));
*returnSize = matrixSize * (*matrixColSize);
int m = 0; // 行数
int n = 0; // 列数
int top = 0;
int bottom = matrixSize - 1;
int left = 0;
int right = (*matrixColSize) - 1;
/*
* direction = 0, right
* direction = 1, down/bottom
* direction = 2, left
* direction = 3, up/top
**/
int direction = 0;
for (int i = 0; i < matrixSize * (*matrixColSize); i++) {
switch(direction) {
case 0:
res[i] = matrix[m][n];
// n == right
if (n == right) {
direction = 1;
m++; // 下移
top++; // 边界下移
} else {
n++;
}
break;
case 1:
res[i] = matrix[m][n];
// m == bottom
if (m == bottom) {
direction = 2;
n--; // 左移
right--; // 边界左移
} else {
m++;
}
break;
case 2:
res[i] = matrix[m][n];
// n == left
if (n == left) {
direction = 3;
m--;
bottom--;
} else {
n--;
}
break;
case 3:
res[i] = matrix[m][n];
// m == top
if (m == top) {
direction = 0;
n++;
left++;
} else {
m--;
}
break;
default:
break;
}
} return res;
}
59. 螺旋矩阵 II
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/ enum {
RIGHT,
DOWN,
LEFT,
UP
}; int** generateMatrix(int n, int* returnSize, int** returnColumnSizes)
{
// 申请内存和初始化
*returnSize = n;
*returnColumnSizes = malloc(sizeof(int) * n);
int **res = malloc(sizeof(int*) * n);
for (int i = 0; i < n; i++) {
res[i] = malloc(sizeof(int) * n);
memset(res[i], 0, sizeof(int) * n);
(*returnColumnSizes)[i] = n;
} int direction = RIGHT;
int row = 0, col = 0;
int top = 0, bottom = n - 1, left = 0, right = n - 1;
for (int num = 1; num <= n * n; num++) {
res[row][col] = num;
switch(direction) {
case RIGHT:
if (col == right) {
direction = DOWN;
top++;
row++;
} else {
col++;
}
break;
case DOWN:
if (row == bottom) {
direction = LEFT;
right--;
col--;
} else {
row++;
}
break;
case LEFT:
if (col == left) {
direction = UP;
bottom--;
row--;
} else {
col--;
}
break;
case UP:
if (row == top) {
direction = RIGHT;
left++;
col++;
} else {
row--;
}
break;
}
} return res;
}
C语言刷“矩阵”类题目(2维矩阵/2级指针)的更多相关文章
- java几个经典的算法题目----------二维矩阵算法
public class testClockwiseOutput { public static void main(String[] args) { //1.构建矩阵数据 int[][] arr = ...
- leetcode刷题-74搜索二维矩阵
题目 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列.每行的第一个整数大于前一行的最后一个整数.示例 1: 输入:matrix ...
- leetcode.矩阵.240搜索二维矩阵II-Java
1. 具体题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性:每行的元素从左到右升序排列:每列的元素从上到下升序排列. 示例: 现有矩阵 ...
- C语言经典算法 - 多维矩阵转一维矩阵的代码
下边内容内容是关于C语言经典算法 - 多维矩阵转一维矩阵的内容,应该能对码农也有好处. #include <stdio.h>#include <stdlib.h>int mai ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- OpenGL矩阵类(C++)
概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例:投影矩阵 概述 OpenGL固定功能管线提供4个不同类型的矩阵(GL_MODELVIEW.GL_PROJECTION. ...
- 精解Mat类(一):基本数据类型-固定大小的 矩阵类(Matx) 向量类(Vector)
一.基础数据类型 1.(基础)固定大小矩阵类 matx 说明: ① 基础矩阵是我个人增加的描述,相对于Mat矩阵类(存储图像信息的大矩阵)而言. ② 固定大小矩阵类必须在编译期间就知晓其维 ...
- OpenGL矩阵类(C++) 【转】
http://www.cnblogs.com/hefee/p/3816727.html OpenGL矩阵类(C++) 概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例: ...
- Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...
随机推荐
- mysql加强(6)~子查询简单介绍、子查询分类
一.子查询简单介绍 1.什么是子查询? 一个查询之中嵌套了其他的若干查询. 在使用select 语句查询时,有时候where的查询条件中的限制条件不是一个确定的值,而是一个来自于另一个查询的结果. 子 ...
- hexo博客如何插入图片
Hexo是一个静态的博客网站生成器,生成一个博客只需要分分钟的时间就能搞定. Hexo的博文是支持Markdown格式的,发表一篇文章只需要简简单单的几个命令. hexo new '文章'就会生成一个 ...
- Android开发-记账本-实现记账功能选择
制作GridView适配器,实现页面数据的变化 制作类型存储数据库,存储的主要是图片类型,类型被选中时的图片,类型未被选中时的图片. 数据库代码如下 package com.example.Utils ...
- 计算机网络再次整理————tcp[二]
前言 本文不会去介绍tcp的具体协议,因为这个tcp 应该不能说是单纯的连接和传输数据这么简单,里面还有很多机制. 正文 首先介绍一下什么是协议族(protocal Family),举个例子PF_IN ...
- 1.Flink实时项目前期准备
1.日志生成项目 日志生成机器:hadoop101 jar包:mock-log-0.0.1-SNAPSHOT.jar gmall_mock |----mock_common |----mock ...
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- npm 查看一个包的版本信息
有了npm 我们能够简单的一段代码就下载我们需要的包,但是包是不断更新的, 所以我们要关注包的版本信息: 现在,假设我们需要 jquery ,但是jquery现在有很多版本,我们如何通过npm查看呢? ...
- 生成树协议(STP)的精髓知识
STP生成树协议 1.STP介绍 2.STP生成树算法 1.STP - Spanning tree protocol (生成树协议)是逻辑上断开环路,防止广播风暴的产生.当线路故障,阻塞接口 ...
- 猪齿鱼平台常用前端css实现方案
居中 最常用的height + line-height,以及margin:0 auto的居中方式就不再阐述,以下介绍两种容错性高的实现方案. flex布局实现 猪齿鱼前端日常开发中,我们多以fle ...
- html页面动效
找到了一个喜欢的黑客帝国动效"https://files.cnblogs.com/files/blogs/718959/codeMatrix-master.zip?t=1643081202& ...