当前位置:网站首页>leetcode-59. Spiral matrix II JS
leetcode-59. Spiral matrix II JS
2022-06-11 12:18:00 【Qianfan at the front】
subject

Code
var generateMatrix = function(n) {
let matrix = []
for (let i = 0; i < n; i++) {
let arr = new Array(n).fill(0)
matrix.push(arr)
}
let num = 1
let upper_bound = 0, right_bound = n-1;
let lower_bound = n-1, left_bound = 0;
while(num <= n*n) {
// From left to right
if(upper_bound <= lower_bound) {
for (let i = left_bound; i <= right_bound; i++) {
matrix[upper_bound][i] = num++
}
upper_bound++
}
// From top to bottom
if(right_bound >= left_bound) {
for (let i = upper_bound; i <= lower_bound; i++) {
matrix[i][right_bound] = num++
}
right_bound--
}
// From right to left
if(lower_bound >= upper_bound) {
for (let i = right_bound; i >= left_bound; i--) {
matrix[lower_bound][i] = num++
}
lower_bound--
}
// From bottom to top
if(left_bound <= right_bound) {
for (let i = lower_bound; i >= upper_bound; i--) {
matrix[i][left_bound] = num++
}
left_bound++
}
}
return matrix
};
Reference material
边栏推荐
- The secret behind the Splunk bucket
- Record a CODIS memory cleanup
- gocron 定时任务管理平台
- flink Spark 和 Flink对比
- 反射真的很耗时吗,反射 10 万次,耗时多久。
- When I saw the sudden death of a 28 year old employee, I wanted to moisten
- 【LeetCode】494. Objective and (2 wrong questions)
- Splunk certificate expired, making kV store unable to start
- 軟件項目管理 7.1.項目進度基本概念
- POJ 3278 catch the cow (width first search, queue implementation)
猜你喜欢

C reads TXT file to generate word document

How does data age in Splunk?

Gestion de projets logiciels 7.1. Concept de base du calendrier du projet

Flash framework web development video notes

Solving the problem of data garbled in sqlserver connection database (Chinese table)

Flink deployment mode and runtime architecture (session mode, single job mode, application mode, jobmanager, taskmanager, yarn mode deployment and runtime architecture)

flink 时间语义、水位线(Watermark)、生成水位线、水位线的传递

C # set or verify the format of text field in PDF

9、聊聊ThreadLocal

阶乘后的零(C语言)
随机推荐
Gocron scheduled task management platform
Flink window table valued function
Yarn switch ResourceManager (failed to connect to server:8032 retries get failed due to exceeded maximum)
Solve the problem of swagger document interface 404
Flink multi stream conversion (side output stream shunting, union, connect) real-time reconciliation of APP payment operations and third-party payment operations
Flink deployment mode and runtime architecture (session mode, single job mode, application mode, jobmanager, taskmanager, yarn mode deployment and runtime architecture)
Record a troubleshooting of MySQL master-slave asynchrony
你管这破玩意儿叫 MQ?
flink 多流转换(侧输出流分流、Union、Connect) 实时对账app 的支付操作和第三方的支付操作的双流 Join
创建线程的四种方式
Yapi installation
China Unicom 22 spring Games Group
让你理解选择排序(C语言)
Flink data flow graph, parallelism, operator chain, jobgraph and executiongraph, task and task slot
flink 数据流图、并行度、算子链、JobGraph与ExecutionGraph、任务和任务槽
The wonderful use of XOR (C language)
中间人攻击之ettercap嗅探
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xc5 in position 13: invalid continuation byte
ftp服务器:serv-u 的下载及使用
8. 18 arhat enhancements for atomic operations