当前位置:网站首页>浅析一道经典题
浅析一道经典题
2022-06-26 06:37:00 【The code family】
浅析一道经典题
这是acwing平台上的语法题里面的一道题目,初学的时候觉得有点难度,现在拿出来看看觉得还蛮有意思,多这些这种题目的确增加些刷题的乐趣hh。。。经典值得反复康康。。(而且发现偏移量和曼哈顿距离这种经典都可以写写博客拓展帮助记忆)
其实是自己的能力也只能分析分析这种题目(手动狗头)。。(努力进步中)
直接上题目(来自于刷题网站acwing)

没错 ,,它就是非常非常经典的“”蛇形矩阵“”。。(如图)

根据题目的意思,输入俩个数 , 输出一个矩阵蛇字排列即可。。无论新手老手肯定想到用数组来比较方便(应该有很多方法可以做)。于是引出需要遍历上下如图:

很多题目都要设置一个偏移量。。如上(非常常用的技巧)
对于一个位置的数,以他为原点建造一个坐标系,写出上下左右的坐标,以0或1表示与这个点水平轴或者纵轴的距离。这样等下就可以设置一个偏移量数组d【x】,这样就可以表示出每次走的时候下一个格子的坐标,方便了很多很多。
还有个问题就是确定需要转向的位置(走到顶部了)这时候就需要改变偏离的方向,利用取余就可达成(d = (d+1)%4)因为总共四个数,这样就能完成转向(偏转90度的目标)。。
所以上代码:

好像这还是微软的面试题。。Leetcode也有个类似的螺旋矩阵。。
偏移量是个非常常用且流弊的工具,看以后学精后能不能把做过的用了后方便很多的题总结出来看看。。
边栏推荐
- PyTorch使用多GPU并行训练及其原理和注意事项
- Thinking skills of technical leaders
- Typescript type
- LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
- 营销技巧:相比较讲产品的优点,更有效的是要向客户展示使用效果
- Message queue - message transaction management comparison
- Research Report on market supply and demand and strategy of Chinese amyl cinnamaldehyde (ACA) industry
- js-下载图片
- Gof23 - prototype mode
- Efk upgrade to Clickhouse log storage practice
猜你喜欢

Understanding of nil in go language

Web technology sharing | webrtc recording video stream

连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)

Efk upgrade to Clickhouse log storage practice
Experience the new features of Milvus 2.0 together

Open source demo| you draw and I guess -- make your life more interesting

GoF23—建造者模式
Installing rainbow in various kubernetes with Helm
New generation engineers teach you how to play with alluxio + ml (Part 1)

Gof23 - builder mode
随机推荐
MySQL delete in without index
PyTorch使用多GPU并行训练及其原理和注意事项
SQL基础
Understanding of nil in go language
Laravel implements groupby to query the number of packets
The four cores of the browser: Trident, gecko, WebKit, blink
Introduction to the use of TS generics in functions, interfaces and classes
API and encapsulation of cookies
Customer Stories | Netease spring breeze: the "spring breeze" of the fun industry, reaching out to all areas through in-depth interaction
SecureCRT运行SparkShell 删除键出现乱码的解法
Go language learning notes 1.1
屏幕共享推荐
Comparison between Prometheus and ZABBIX
How to make the main thread wait for the sub thread to execute before executing
闭包问题C# Lua
浏览器的四大内核:Trident,Gecko,Webkit,Blink
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
解决新版谷歌Chrome浏览器Cookie跨域失效问题
[micro service series] protocol buffer dynamic analysis
遇到女司机业余开滴滴,日入500!