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. kindle序列号对应版本

    序列号前缀 型号全称 型号简称 支持越狱 B001, Kindle 1 K1 - B101 B002 Kindle 2 U.S. (Sprint) K2 - B003 Kindle 2 Interna ...

  2. K8s 资源配额管理对象 ResourcesQuota

    Kubernetes 是一个多租户平台,更是一个镜像集群管理工具.一个 Kubernetes 集群中的资源一般是由多个团队共享的,这时候经常要考虑的是如何对这个整体资源进行分配.在 kubernete ...

  3. 搭建sublime.txt环境结合使用python

    { "cmd": ["python3", "-u", "$file"] }

  4. 安卓开发常见Bug-setContentView(R.layout.....)报错

    这是安卓开发的常见错误,当你在引用或者复制别人的Layout xml文件时需要在AndroidManifest.xml中添加东西 需要将图中的activity android:name添加进去,否则是 ...

  5. NoSuchMethodError错误

    发生原因: 一个项目中包含有相同名字,但内容不同的包 解决办法:删除其中暂时不用的包 后记:如果不知道哪一个包是多余的,直接用IDE查找:找到该类,然后将该包进行反编译,再次导入该项目,再找到该类,出 ...

  6. 微信h5下拉隐藏网页,还有取消页面滑动

    需求: 网页下拉太丑了,如下 度娘了一下, 发现一篇相关文档 基本解决了问题 https://juejin.cn/post/6844903940190896135#heading-2 加入如下代码即可 ...

  7. 基于Autolayout的动画

    在修改了约束之后,只要执行下面代码,就能做动画效果 [UIView animateWithDuration:1.0 animations:^{ [添加了约束的view的父控件 layoutIfNeed ...

  8. shell脚本之循环语句与函数

    shell脚本之循环语句与函数 echo的用法: echo -n #表示不换行输出 echo -e #输出转义字符,将转义后的内容输出到屏幕上 转义字符: \n :换行,被输出的字符从"\n ...

  9. rpm与yum安装及管理程序

    安装及管理程序 1.Linux应用程序基础 2.RPM软件包管理工具 3.yum源仓库创建 1.应用程序与系统命令的关系如图:  典型应用程序的目录结构如图: 常见的软件包封装类型如图: 2.RPM包 ...

  10. Docker prefereces

    https://docs.docker.com/docker-for-mac/#preferences-menu docker 的镜像命令需要抽时间了解