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. SQL解析器详解

    1.概述 最近,有同学留言关于SQL解析器方面的问题,今天笔者就为大家分享一下SQL解析器方便的一些内容. 2.内容 2.1 SQL解析器是什么? SQL解析与优化是属于编辑器方面的知识,与C语言这类 ...

  2. ApacheCN Asp.NET 译文集 20211126 更新

    ASP.NET Core2 基础知识 零.前言 一.搭建舞台 二.控制器 三.视图 四.模型 五.验证 六.路由 七.RestBuy 八.添加功能.测试和部署 ASP.NET Core3 和 Angu ...

  3. ApacheCN NodeJS 译文集 20211204 更新

    Node API 开发入门指南 零.前言 一.Node.js 简介 二.构建 API--第 1 部分 三.构建 API--第 2 部分 React TypeScript Node 全栈开发 零.序言 ...

  4. JVM学习十二 - (复习)JVM内存结构

    JVM 内存结构 Java 虚拟机的内存空间分为 5 个部分: 程序计数器 Java 虚拟机栈 本地方法栈 堆 方法区 JDK 1.8 同 JDK 1.7 比,最大的差别就是:元数据区取代了永久代.元 ...

  5. 使用java实现圆形运动

    1 package com.neuedu.test; 2 3 import java.awt.Graphics; 4 import java.awt.Image; 5 6 import com.neu ...

  6. HEAAN库学习

    本文主要学习HEAAN同态库,选择最老的一版:地址,最新版在:位置,HEAAN是CKKS17论文的主要代码复现. 版本 1.地址这是最老的一版,对应的论文CKKS17 2.在1的基础上,实现了boot ...

  7. E开始使用webdriver

    GETTING STARTED WITH WEBDRIVER Selenium supports automation of all the major browsers in the market ...

  8. 帆软报表(finereport)控件背景色更改

    setTimeout(function() { $('.fr-trigger-btn-up').css({ "background-color": "#003399&qu ...

  9. pycharm软件安装

    目前热门的编程软件: 1.VScode 小巧轻便但是对小白不是很优化 2.sublime 时下最流行的代码编辑器,功能十分强大可以运行在windows.macOS和linux系统中,小白先不要使用 3 ...

  10. TCP/IP详解 读书笔记:TCP:传输控制协议

    TCP的服务 TCP为应用层提供一种面向连接的.可靠的字节流服务. 一个TCP连接中,仅有两方进行彼此通信,所以广播和多播不能用于TCP. TCP通过以下方式提供可靠性: 应用数据被切割为TCP认为最 ...