当前位置: 首页 > news >正文

[LeetCode]Spiral Matrix

题目:Spiral Matrix

螺旋输出一个数组。

思路:

螺旋的方式遍历这个数组,一次外循环,遍历数组一圈,直到全部遍历完。

/***************************************************************************************************
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
***************************************************************************************************/
#include<stdio.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
    int *array = (int *)malloc(matrixRowSize*matrixColSize*sizeof(int));
    int rowUp = 0,rowLow = matrixColSize - 1,columnUp = 0,columnLow = matrixRowSize - 1;
    int i,index = 0;
    while(rowUp < rowLow && columnUp < columnLow){
        for(i = rowUp;i < rowLow;i++){//行的上下界
            array[index++] = matrix[columnUp][i];
        }
        for(i = columnUp;i < columnLow;i++){//列的上下界
            array[index++] = matrix[i][rowLow];
        }
        for(i = rowLow;i > rowUp;i--){
            array[index++] = matrix[columnLow][i];
        }
        for(i = columnLow;i > columnUp;i--){
            array[index++] = matrix[i][rowUp];
        }
        columnUp++;
        rowUp++;
        columnLow--;
        rowLow--;
      }
      if(rowUp == rowLow){//还剩一列
          for(i = columnUp;i <= columnLow;i++)
              array[index++] = matrix[i][rowUp];
      }else if(columnUp == columnLow){//还剩一行
          for(i = rowUp;i <= rowLow;i++)
              array[index++] = matrix[columnUp][i];
      }
      return array;
}

void main(){
    int num = 5;
    int **a = (int **)malloc(num*sizeof(int *));
    for(int i = 0;i < num;i++){
        a[i] = (int *)malloc(num*sizeof(int));
        for(int j = 0;j < num;j++){
            a[i][j] = i*num + j + 1;
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    int *ret = spiralOrder(a,num,num);
    for(int i = 0;i < num*num;i++){
        printf("%d ",ret[i]);
    }
    free(ret);
    for(int i = 0;i < num;i++){
        free(a[i]);
    }
    free(a);
}

 题目:Spiral MatrixII

给定一个数字n,则有n^2个数字,螺旋的形式将这些数字分布到n单元的数组中。

思路:

和上面类似一圈为一个周期,但是这个数组是nxn的不会有行列不等的情况。

package com.example.medium;

/**
 * Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
 *For example,
 *Given n = 3,
 *You should return the following matrix:
 *[
 * [ 1, 2, 3 ],
 * [ 8, 9, 4 ],
 * [ 7, 6, 5 ]
 *]
 * @author FuPing
 *
 */
public class GenerateMatrix {
    public static int[][] generateMatrix(int n){
        if(n < 0)return null;
        int [][]matrix = new int[n][n];
        int up = 0,low = n - 1,i,index = 1;//index数字的值
        while(up < low){
            for(i = up;i < low;i++){//up表示行标下界,low表示列标的上界
                matrix[up][i] = index++;
            }
            for(i = up;i < low;i++){
                matrix[i][low] = index++;
            }
            for(i = low;i > up;i--){
                matrix[low][i] = index++;
            }
            for(i = low;i > up;i--){
                matrix[i][up] = index++;
            }
            up++;
            low--;
        }
        if(up == low)matrix[up][up] = index++;//n为奇数时
        return matrix;
    }
    
    public static void main(String args[]){
        int n = 0;
        int [][]a = generateMatrix(n);
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                System.out.print(a[i][j] + "  ");
            }
            System.out.println();
        }
    }
}

 

转载于:https://www.cnblogs.com/yeqluofwupheng/p/6678988.html

相关文章:

  • NOIP2016 天天爱跑步 正解
  • 如何选用持久化存储方案--一些需要考虑的问题
  • C语言求最小公倍数和最大公约数三种算法(经典)
  • 搜索:文本的匹配算法
  • 应用解决告诉你什么时候该用ajax
  • 1154: 零起点学算法61——矩阵转置
  • 20155224 实验一《Java开发环境的熟悉》实验报告
  • 奇偶排序
  • Oracle分组取第一条数据
  • 听说你叫Java(二)–Servlet请求
  • BZOJ 3172 Tjoi2013 单词 后缀数组
  • C#基础_MD5
  • Protobuf3 语法指南
  • Oracle数据库服务器IO高的分析方案和案例探讨
  • yii2清空模态框表单的数据,每次点击开始之前让数据清空
  • 【Linux系统编程】快速查找errno错误码信息
  • cookie和session
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • JavaScript 奇技淫巧
  • MD5加密原理解析及OC版原理实现
  • Theano - 导数
  • 关于 Cirru Editor 存储格式
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 如何进阶一名有竞争力的程序员?
  • 双管齐下,VMware的容器新战略
  • 算法系列——算法入门之递归分而治之思想的实现
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 智能合约Solidity教程-事件和日志(一)
  • ​比特币大跌的 2 个原因
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (笔试题)合法字符串
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (分布式缓存)Redis分片集群
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (一)appium-desktop定位元素原理
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • 、写入Shellcode到注册表上线
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET CLR基本术语
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net 获取url的方法
  • .NET 依赖注入和配置系统
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • :not(:first-child)和:not(:last-child)的用法
  • @Data注解的作用
  • @RequestParam,@RequestBody和@PathVariable 区别
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [Android]如何调试Native memory crash issue
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [BZOJ2850]巧克力王国
  • [CSS]中子元素在父元素中居中
  • [CTSC2014]企鹅QQ
  • [Godot] 3D拾取
  • [IT生活推荐]大家一起来玩游戏喽,来的都进!