当前位置:网站首页>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;
}
}
边栏推荐
- uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
- "Xiaodeng" domain password policy enhancer in operation and maintenance
- Paddleocr tutorial
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- IELTS examination process, what to pay attention to and how to review?
- URLs and URIs
- 巩固表达式C# 案例简单变量运算
- How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
- 业务场景功能的继续修改
- Detailed explanation of openharmony resource management
猜你喜欢
[IELTS reading] Wang Xiwei reading P4 (matching1)
Significance of acrel EMS integrated energy efficiency platform in campus construction
Application of fire fighting system based on 3D GIS platform
Jar batch management gadget
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
用快解析内网穿透实现零成本自建网站
基于三维gis平台的消防系统运用
abc 258 G - Triangle(bitset)
【路径规划】RRT增加动力模型进行轨迹规划
随机推荐
Jar batch management gadget
Meet ThreadPoolExecutor
AcWing164. 可达性统计(拓扑排序+bitset)
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Hash table, hash function, bloom filter, consistency hash
(script) one click deployment of any version of redis - the way to build a dream
Is it safe to open an account in the College of Finance and economics? How to open an account?
如何有效对直流列头柜进行监测
Chinese verification of JS regular expressions (turn)
Instructions for go defer
A new method for analyzing the trend chart of London Silver
XML的解析
城市轨道交通站应急照明疏散指示系统设计
P4408 [NOI2003] 逃学的小孩(树的直径)
Nine Qi single chip microcomputer ny8b062d single key control four LED States
P3304 [sdoi2013] diameter (diameter of tree)
C语言中sizeof操作符的坑
[Peking University] tensorflow2.0-1-opening
In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
GDB常用命令