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级指针)的更多相关文章

  1. java几个经典的算法题目----------二维矩阵算法

    public class testClockwiseOutput { public static void main(String[] args) { //1.构建矩阵数据 int[][] arr = ...

  2. leetcode刷题-74搜索二维矩阵

    题目 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列.每行的第一个整数大于前一行的最后一个整数.示例 1: 输入:matrix ...

  3. leetcode.矩阵.240搜索二维矩阵II-Java

    1. 具体题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性:每行的元素从左到右升序排列:每列的元素从上到下升序排列. 示例: 现有矩阵 ...

  4. C语言经典算法 - 多维矩阵转一维矩阵的代码

    下边内容内容是关于C语言经典算法 - 多维矩阵转一维矩阵的内容,应该能对码农也有好处. #include <stdio.h>#include <stdlib.h>int mai ...

  5. [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 ...

  6. OpenGL矩阵类(C++)

    概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例:投影矩阵 概述 OpenGL固定功能管线提供4个不同类型的矩阵(GL_MODELVIEW.GL_PROJECTION. ...

  7. 精解Mat类(一):基本数据类型-固定大小的 矩阵类(Matx) 向量类(Vector)

    一.基础数据类型 1.(基础)固定大小矩阵类 matx 说明: ①    基础矩阵是我个人增加的描述,相对于Mat矩阵类(存储图像信息的大矩阵)而言. ②    固定大小矩阵类必须在编译期间就知晓其维 ...

  8. OpenGL矩阵类(C++) 【转】

    http://www.cnblogs.com/hefee/p/3816727.html OpenGL矩阵类(C++) 概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例: ...

  9. Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...

随机推荐

  1. kubernetes之Pod水平自动伸缩(HPA)

    https://k8smeetup.github.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/ Horizon ...

  2. java 变量的定义 类型转换 基本的数据类型

    package com.aaa.zxf.ajax.test; import org.junit.Test; /** * 六. * 如何在ideal的maven项目中导入 Test 测试类? * * * ...

  3. 基础学习:关于this在派生类构造函数中的理解

    https://www.cnblogs.com/Bear-Study-Hard/archive/2006/01/09/313551.html 看了上面这篇文章有感,特做了个小样板,以加深对于this在 ...

  4. Tomcat启动报错org.apache.catalina.core.StandardContext listenerStart

    感谢原文作者:西北码农 原文链接:https://blog.csdn.net/bitree1/article/details/72236633 解决方法: 1.检查配置信息有无异常:如 web.xml ...

  5. Docker容器启动失败 Failed to start Docker Application Container Engine

    1.在k8s mster节点执行 1.kubectl get nodes 发现node节点没起来 [root@guanbin-k8s-master ~]# kubectl get nodes NAME ...

  6. 在TCP文件传输中如何判断java流的末尾

    感谢前辈们的解答:https://bbs.csdn.net/topics/280085530 问题描述: 服务端向客户端发送数据流,服务端发完了数据不关闭流. 我在客户端读流,我无法读到-1,所以无法 ...

  7. vue 中 使用 element-ui 发送请求前 校验全部表单,报警告: [Element Warn][Form]model is required for validate to work!

    WEB先生 2020-07-14 20:01:45  754  收藏 分类专栏: vue 文章标签: vue js 版权 报这种错可能有以下两种情况 1.属性绑定错误,确保绑定的是  :model  ...

  8. NSDictionary基本概念

    1.NSDictionar基本概念 什么是NSDictionary NSDictionary翻译过来叫做"字典" 日常生活中,"字典"的作用:通过一个拼音或者汉 ...

  9. play的action链(一个action跳转到另一个action,类似于重定向)

    在play中没有Servlet API forward 的等价物.每一个HTTP request只能调用一个action.如果我们需要调用另一个,必须通过重定向,让浏览器访问另一个URL来访问它.这样 ...

  10. 无脑安装java编程语言开发的集成环境 IDEA 白嫖版本

    无脑安装java编程语言开发的集成环境  IDEA 根python很类似:可以借鉴无脑安装--Python 及 安装python集成开发环境pycharm - 隐姓埋名4869 - 博客园 (cnbl ...