力扣之603.连续空余座位

文章目录

  • [1. 603.连续空余座位](#1. 603.连续空余座位)
    • [1.1 题干](#1.1 题干)
    • [1.2 准备数据](#1.2 准备数据)
    • [1.3 思路分析](#1.3 思路分析)
    • [1.4 解法](#1.4 解法)
    • [1.5 结果截图](#1.5 结果截图)

1. 603.连续空余座位

1.1 题干

表: Cinema

±------------±-----+

| Column Name | Type |

±------------±-----+

| seat_id | int |

| free | bool |

±------------±-----+

Seat_id 是该表的自动递增主键列。

在 PostgreSQL 中,free 存储为整数。请使用 ::boolean 将其转换为布尔格式。

该表的每一行表示第 i 个座位是否空闲。1 表示空闲,0 表示被占用。

查找电影院所有连续可用的座位。

返回按 seat_id 升序排序 的结果表。

测试用例的生成使得两个以上的座位连续可用。

结果表格式如下所示。

示例 1:

输入:

Cinema 表:

±--------±-----+

| seat_id | free |

±--------±-----+

| 1 | 1 |

| 2 | 0 |

| 3 | 1 |

| 4 | 1 |

| 5 | 1 |

±--------±-----+

输出:

±--------+

| seat_id |

±--------+

| 3 |

| 4 |

| 5 |

±--------+

1.2 准备数据

sql 复制代码
Create table If Not Exists Cinema (seat_id int primary key auto_increment, free bool)
Truncate table Cinema
insert into Cinema (seat_id, free) values ('1', '1')
insert into Cinema (seat_id, free) values ('2', '0')
insert into Cinema (seat_id, free) values ('3', '1')
insert into Cinema (seat_id, free) values ('4', '1')
insert into Cinema (seat_id, free) values ('5', '1')

1.3 思路分析

1.4 解法

sql 复制代码
select distinct c2.seat_id from Cinema c1,Cinema c2 where abs(c1.seat_id-c2.seat_id)=1 and c1.free=1 and c2.free=1;

1.5 结果截图

相关推荐
滴图服务-七七26 分钟前
滴滴地图:精准定位赋能企业数字化转型
大数据·人工智能·地图服务·甲级测绘资质·商业授权
devilnumber2 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
北顾笙9803 小时前
MySQL-day2
数据库·mysql
‎ദ്ദിᵔ.˛.ᵔ₎3 小时前
双指针、滑动窗口、前缀和、二分查找 算法
算法
顾北顾4 小时前
多头注意力机制
人工智能·深度学习·算法
H178535090964 小时前
SolidWorks_基于草图的实体特征20_特征错误排查
算法·3d建模·solidworks
hujinyuan201604 小时前
2025年12月中国电子学会青少年机器人技术等级考试试卷(二级) 真题+答案
人工智能·算法·机器人
程序猿乐锅5 小时前
【MySQL | 第八篇】MySQL 视图
数据库·mysql
bIo7lyA8v5 小时前
算法复杂度评估的实验统计方法与可视化的技术8
算法
李老师讲编程5 小时前
中国电子学会图形化2020.12月Scratch三级考级题
算法·scratch·信息学奥赛·图形化编程·scratch素材