当前位置:网站首页>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;
}
}
边栏推荐
- 积分商城游戏设置的基本要点
- 用快解析内网穿透实现零成本自建网站
- P3304 [sdoi2013] diameter (diameter of tree)
- Skills in analyzing the trend chart of London Silver
- abc 258 G - Triangle(bitset)
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- The pit of sizeof operator in C language
- 城市轨道交通站应急照明疏散指示系统设计
- 如何避免电弧产生?—— AAFD故障电弧探测器为您解决
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
猜你喜欢
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
How to use fast parsing to make IOT cloud platform
js如何实现数组转树
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
Skills in analyzing the trend chart of London Silver
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
雅思考试流程、需要具体注意些什么、怎么复习?
Fast analysis -- easy to use intranet security software
It's too convenient. You can complete the code release and approval by nailing it!
随机推荐
Consolidated expression C case simple variable operation
实战模拟│JWT 登录认证
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
abc 258 G - Triangle(bitset)
【雅思阅读】王希伟阅读P4(matching1)
Basic points of the game setup of the points mall
(script) one click deployment of any version of redis - the way to build a dream
他做国外LEAD,用了一年时间,把所有房贷都还清了
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
挖财学院开户安全的吗?开户怎么开?
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
P3304 [sdoi2013] diameter (diameter of tree)
Cross domain request
图解网络:什么是网关负载均衡协议GLBP?
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent