当前位置:网站首页>淺析一道經典題
淺析一道經典題
2022-06-26 06:42:00 【The code family】
淺析一道經典題
這是acwing平臺上的語法題裏面的一道題目,初學的時候覺得有點難度,現在拿出來看看覺得還蠻有意思,多這些這種題目的確增加些刷題的樂趣hh。。。經典值得反複康康。。(而且發現偏移量和曼哈頓距離這種經典都可以寫寫博客拓展幫助記憶)
其實是自己的能力也只能分析分析這種題目(手動狗頭)。。(努力進步中)
直接上題目(來自於刷題網站acwing)

沒錯 ,,它就是非常非常經典的“”蛇形矩陣“”。。(如圖)

根據題目的意思,輸入倆個數 , 輸出一個矩陣蛇字排列即可。。無論新手老手肯定想到用數組來比較方便(應該有很多方法可以做)。於是引出需要遍曆上下如圖:

很多題目都要設置一個偏移量。。如上(非常常用的技巧)
對於一個比特置的數,以他為原點建造一個坐標系,寫出上下左右的坐標,以0或1錶示與這個點水平軸或者縱軸的距離。這樣等下就可以設置一個偏移量數組d【x】,這樣就可以錶示出每次走的時候下一個格子的坐標,方便了很多很多。
還有個問題就是確定需要轉向的比特置(走到頂部了)這時候就需要改變偏離的方向,利用取餘就可達成(d = (d+1)%4)因為總共四個數,這樣就能完成轉向(偏轉90度的目標)。。
所以上代碼:

好像這還是微軟的面試題。。Leetcode也有個類似的螺旋矩陣。。
偏移量是個非常常用且流弊的工具,看以後學精後能不能把做過的用了後方便很多的題總結出來看看。。
边栏推荐
- Custom reference formats used by Zotero
- Message queue - message transaction management comparison
- 【golang】time相关
- Temperature alarm
- Bugku练习题---MISC---富强民主
- Transaction and message semantics
- 【微服务系列】Protocol buffer动态解析
- [golang] time related
- 面试官:测试计划和测试方案有什么区别?
- [spark] how to implement spark SQL field blood relationship
猜你喜欢

MYSQL触发器要如何设置,简单教程新手一看就会
Web components series (10) -- realize the basic layout of mycard

STM 32 uses cube to generate Tim to trigger ADC and transmit through DMA

MySQL 数据库的小白安装与登录

Install pyinstaller

Live broadcast Preview - fire safety instructor training "cloud class" is about to start!

成水最多的容器

Temperature alarm

Understanding of nil in go language

Pytorch uses multi GPU parallel training and its principle and precautions
随机推荐
Experience the new features of Milvus 2.0 together
Research Report on market supply and demand and strategy of natural organic beauty industry in China
个人博客系统需求分析
Installing rainbow in various kubernetes with Helm
Pytorch mixing accuracy principle and how to start this method
[digital signal processing] basic sequence (unit step sequence | relationship between unit step sequence and unit pulse sequence | rectangular sequence | relationship between rectangular sequence and
Laravel implements groupby to query the number of packets
Gof23 - prototype mode
GoF23—抽象工厂模式
Closure problem C Lua
How to select and build a real-time data warehouse scheme
Ppt template crawler case
Message queuing - omnidirectional comparison
SHOW语句用法补充
Install pyinstaller
Type de Typescript
Lightgbm-- parameter adjustment notes
MYSQL索引不生效的原因
Go language learning notes 1.2- variables
How to set MySQL triggers is a simple tutorial for novices