当前位置:网站首页>2022.07.03 (lc_6111_counts the number of ways to place houses)
2022.07.03 (lc_6111_counts the number of ways to place houses)
2022-07-05 00:14:00 【Leeli9316】

Method : simulation
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int[][] spiralMatrix(int m, int n, ListNode head) {
int[][] res = new int[m][n];
int up = 0, down = m - 1, left = 0, right = n - 1;
while (true) {
// From left to right
for (int col = left; col <= right; col++) {
if (head != null) {
res[up][col] = head.val;
head = head.next;
} else {
res[up][col] = -1;
}
}
if (++up > down) break;
// From top to bottom
for (int row = up; row <= down; row++) {
if (head != null) {
res[row][right] = head.val;
head = head.next;
} else {
res[row][right] = -1;
}
}
if (--right < left) break;
// From right to left
for (int col = right; col >= left; col--) {
if (head != null) {
res[down][col] = head.val;
head = head.next;
} else {
res[down][col] = -1;
}
}
if (--down < up) break;
// From bottom to top
for (int row = down; row >= up; row--) {
if (head != null) {
res[row][left] = head.val;
head = head.next;
} else {
res[row][left] = -1;
}
}
if (++left > right) break;
}
return res;
}
}边栏推荐
- PMP certificate renewal process
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- 【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“
- Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
- 基本放大电路的学习
- 2022.07.03(LC_6109_知道秘密的人数)
- 青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
- Consolidated expression C case simple variable operation
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- 如何避免电弧产生?—— AAFD故障电弧探测器为您解决
猜你喜欢
随机推荐
Is the account opening link of Huatai Securities with low commission safe?
GDB common commands
跨域请求
[monitoring] ZABBIX
P3304 [SDOI2013]直径(树的直径)
IELTS examination process, what to pay attention to and how to review?
Some basic functions of enterprise projects are developed, and important things are saved to online first a
js如何实现数组转树
如果炒股开华泰证券的户,在网上开户安全吗?
Actual combat simulation │ JWT login authentication
AcWing164. 可达性统计(拓扑排序+bitset)
【监控】zabbix
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
Detailed explanation of openharmony resource management
URLs and URIs
Design of emergency lighting evacuation indication system for urban rail transit station
用快解析内网穿透实现零成本自建网站
2022.07.03(LC_6108_解密消息)
Hash table, hash function, bloom filter, consistency hash









![[IELTS reading] Wang Xiwei reading P4 (matching1)](/img/91/1b3f85410035f65acb0c205185f698.png)