LeetCode 54. Spiral Matrix

一文梳理金融风控建模全流程(Python) 。借助A卡模型,银行或金融机构能够根据申请人的个人信息和历史数据,对其进行风险评估。这种评估能够帮助金融机构判断借款人是否具备偿还贷款的能力和意愿。 阅读详情

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].



The line marked as red need to pay special attention! (It is very easy to forget it!)

I personally think this question is pretty hard...... It has a lot of details to deal with in a limited time and easy to make mistakes.

#include <vector>
#include <iostream>
using namespace std;

vector<int> spiralOrder(vector< vector<int> >& matrix) {
    if(matrix.size() == 0) return {};
    if(matrix[0].size() == 0) return {};
    int m = matrix.size();
    int n = matrix[0].size();
    vector<int> res;

    int layer = m < n ? m : n;
    // this is only for even m and n. 
    for(int i = 0; i < layer/2; ++i) {
        for(int j = i; j < n - 1 - i; ++j) {
            res.push_back(matrix[i][j]);
        }

        for(int j = i; j < m - 1 - i; ++j)
            res.push_back(matrix[j][n-1-i]);

        for(int j = n - 1 - i; j > i; --j)
            res.push_back(matrix[m-1-i][j]);

        for(int j = m - 1 - i; j > i; --j)
            res.push_back(matrix[j][i]);
    }
    // ends for even m and n.
    if(layer % 2 != 0) {
        if(m > n) {
            for(int i = layer/2; i <= m - 1 - layer/2; ++i) {
                res.push_back(matrix[i][n/2]);
            }
        } else {
            for(int i = layer/2; i <= n - 1 - layer/2; ++i) {
                res.push_back(matrix[m/2][i]);
            }
        }
    }
    return res;
}
 int main(void) {
    vector< vector<int> > matrix{
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5}};
    vector<int> res = spiralOrder(matrix);
    for(int i = 0; i < res.size(); ++i) {
         cout << "index i: " << i << " " << res[i] << endl;
    }
    cout << endl;
}


2: Print matrix diagonally.

/*
  1   2  3
  4   5  6
  7   8  9
  10 11 12

  the output should be: 
  1
  4  2
  7  5 3
  10 8 6
  11 9
  12
*/
void printDiagional(vector< vector<int> >& matrix) {
  int rows = matrix.size();
  int cols = matrix[0].size();
  for(int k = 0; k < rows; ++k) {
    int row = k, col = 0;
    while(row >= 0 && col <= min(cols - 1, k)) {
      cout << matrix[row][col] << " ";
      row--;
      col++;
    }
    cout << endl;
  }
  for(int k = 1; k < cols; ++k) {
    int row = 0, col = k;
    while((col <= cols - 1) && row <= min(rows, cols - 1 - k)) {
      cout << matrix[rows - 1 - row][col] << " ";
      col++;
      row++;
    }
    cout << endl;
  }
}

int main(void) {
  vector< vector<int> > matrix {
    {10, 11}};
  printDiagional(matrix);
}

2022年电赛F题23年电赛D题-信号调制度测量装置说明中提到带通采样定律。 另外,当对于一个频率很高的射频信号采样时,如果采样频率设的太低,对提高采样量化的信噪比是不利的。低通采样定理在《信号与系统》教材中进行了详细的讨论,但未对带通信号的采样定理进行描述,国内樊昌信和曹丽娜老师编写的《通信原理》第10章第2小节有所描述,但没有详细的公式推导,容易给初学者产生超过该最低采样频率即不会产生频谱混叠的假象。故采样频率没选好,就会使周期性频谱搬移过程中造成频谱混叠,既然频谱和原信号的频谱对不上了,故就无法无失真恢复原信号了,总之,频域波形不出现重叠是无失真传输的重要保障。 阅读详情

相关推荐

手把手教你用UNI-MOL进行分子性质预测:从3D构象到实战结果

本文提供了使用UNI-MOL进行分子性质预测的完整实战指南。通过详细的环境搭建、3D构象生成、模型加载与微调步骤,手把手教你构建端到端的预测流水线。文章重点解析了如何利用UNI-MOL的3D分子表示学习能力,超越传统2D方法,实现对溶解度、活性等性质的更精准预测,并探讨了结果解读与模型分析方法。

weixin_29164091的博客 116

【LeetCode】解题54:Spiral Matrix

LeetCode解题 54:Spiral Matrix Problem 54: Spiral Matrix [Medium]解题思路Solution (Java)修改过程 Problem 54: Spiral Matrix [Medium] Given a matrix of m x n elements (m rows, n columns), return all elements of th...

Fayedily的博客 257

FISCO-BCOS配置及使用控制台

如果因为网络问题长时间无法下载,请尝试:curl -#LO https://gitee.com/FISCO-BCOS/console/raw/master-2.0/tools/download_console.sh && bash download_console.sh。为了可以更加方便快捷,所以HelloWorld合约已经被内置了,位于控制台下面的contracts/solidity/HelloWorld.sol。用于获取和设置name的值,合约代码如下。部署成功会返回合约地址。

qq_51562644的博客 831

LeetCode54——Spiral Matrix

LeetCode54——Spiral Matrix 还是用图来表述: 从左上开始,按照箭头的顺序,进行4个for循环,完成一圈,一共用count = min(row,col)/2  圈。 注意最后的红色箭头,如果行数和列数中较小的数是奇数,就会出现上述情况。 这个时候咱们在最后补上就好 代码: class Solution { public: vector spiralOrd

上善若水 2152

【低轨】Starlink星链-Ku波段OFDM信号同步机制与PNT应用潜力

本文深入解析了Starlink星链Ku波段OFDM信号的同步机制及其在定位、导航与授时(PNT)领域的应用潜力。研究发现,其信号帧内嵌的同步序列具有相位恒定特性,支持超长相干积分,可在极低信噪比下实现高精度伪距测量,为利用低轨卫星通信信号实现高精度PNT服务提供了新的技术路径。

weixin_29250403的博客 729

一步步教你获取ADNI影像数据:从搜索到下载全流程解析

本文详细解析了从ADNI数据库获取阿尔茨海默病影像数据的全流程,包括账号注册、数据检索技巧、高效下载策略及常见问题解决方案。特别介绍了高级搜索的黄金组合和智能数据集管理方法,帮助研究者快速获取T1加权MRI、PET等多模态影像数据,提升科研效率。

weixin_29061465的博客 320

【LeetCode】54. Spiral Matrix

问题描述https://leetcode.com/problems/spiral-matrix/#/description 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

zgljl2012的专栏 1090

FPGA经验谈系列文章——3、逻辑级数

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 Xilinx FPGA开发tips系列——前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的

gzy0506的博客 1万+

[LeetCode 54] Spiral Matrix

题目链接:spiral-matrix import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For

瓦尔登湖 1034

华为MetaERP在财务模块(GL/AP/AR/FA)及子账模块效率优化、月末关账加速、与外部系统对账方面,已形成一套**“并行+实时+智能化”**的完整技术方案

华为MetaERP推出"并行+实时+智能化"技术方案,显著提升财务模块效率:1)采用三级并行策略和内存计算,使子账模块单任务处理时间从142分钟降至9分钟;2)通过并行关账架构将月末关账时间从68小时压缩至7.2小时;3)运用AI智能对账提升效率50%,并支持异步流程和区块链审计。典型场景测试显示月结耗时下降89%,CPU利用率提升116%。方案建议分阶段实施,注意依赖分析、任务粒度设计和性能监控,避免过度并行。该方案已有效解决大型集团财务处理效率低、关账慢、对账难等痛点。

金牌架构师 557

手把手教你学Simulink——基于核能发电系统的场景实例:核电站汽轮机与发电机协调控制仿真

在核电机组中,汽轮机-发电机系统是将核能最终转化为电能的关键环节。与火电机组不同,核反应堆的热惯性大、功率调节慢,因此汽轮机的控制策略需与反应堆特性相匹配,实现安全、稳定、高效的能量转换。维持发电机输出功率/频率稳定(响应电网调度)调节汽轮机进汽压力(匹配一回路蒸汽供应)实现“堆-机协调控制”保障机组在启停、变负荷、甩负荷等工况下的安全性本教程将带你使用构建一个核电站汽轮机与发电机协调控制系统汽轮机调速系统(调门控制)建模发电机与电网接口建模(同步机)

MHD0815的博客 212

leetcode 54. Spiral Matrix 、59. Spiral Matrix II

54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0)、(1,1)这种开始的。 用endx、endy来表示每次转圈的x、y方向的终止位置,方便后面进行边界条件设置。 注意:后面start < endx && start &...

weixin_30374009的博客 120

【乐企-工具篇】有关乐企发票文件生成- OFD和PDF文件生成

有关乐企发票文件生成-OFD和PDF文件生成

九七的博客 2052

leetcode矩阵类题目专杀之54. Spiral Matrix

http://www.fengchang.cc/post/3    

xunileida的专栏 267

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode 54. Spiral Matrix

题目描述:按照螺旋顺序打印矩阵。 题目链接:Leetcode 54. Spiral Matrix 思考思路:就是矩阵的一个搜索遍历,用到思想就是要dx\dy 来控制方向,正如 (0,0,-1,1)这样的方向选择,就是选择不同方向,要思考的就是什么时候转变方向。 代码如下 class Solution(object): def spiralOrder(self, matrix): ...

Za七杂⑧ 271

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

leetcode-54. Spiral Matrix

problem Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]...

学习笔记 318

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode54.Spiral Matrix

LeetCode54.Spiral Matrix

weixin_38665509的博客 362

Leetcode54. Spiral Matrix解题方案

Leetcode54. Spiral Matrix 题目描述 Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] 思路 有四个方向right, left, top, bottom. direction=“right”, 当到达最右边界时,代表本行输出结束,top++, direction=“bottom”,当到达最下边界时, right–, direction = “left”,当到达最左边界是

初七的博客 161

LeetCode[54] Spiral Matrix

LeetCode[54] 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 matri...

weixin_34413357的博客 86

Leetcode 54, Spiral Matrix

Leetcode 54, Spiral Matrix, 难度medium就是按照规定顺序输出一个矩形,用四个变量保存了边界,然后对每一条边进行循环读入,处理一下相应的边界问题,复杂度为O(n),遍历了一遍数组。 以下是代码:class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) {

chiyiyan1586的博客 173

【C++】【LeetCode】54. Spiral Matrix && 59. Spiral Matrix II

题目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

zjajgyy的博客 359

Leetcode 54 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 ] ]

无名山丘,崛起成峰 1018

leetcode【54】Spiral Matrix

问题描述: Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7...

fanyuwgy的博客 118

Leetcode 54. Spiral Matrix and 59. Spiral Matrix II

一:Leetcode 54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 方法一:python切片 if not matrix: return [] ret = [] while matrix: ...

rainMan的博客 216

Leetcode oj java 54. 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,

Xcy的博客 339

【LeetCode 54】 Spiral Matrix

题目描述 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]...

iCode_girl的博客 128
上一篇: LeetCode 251. Flatten 2D Vector
下一篇: LintCode: Fizz Buzz
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值