603. 连续空余座位

力扣题目跳转(603. 连续空余座位 - 力扣(LeetCode)

表: 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       |
+---------+

case 1 的建表语句。

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 然后两个位置差值为1即可。

select distinct
    c1.seat_id
from Cinema c1,Cinema c2
where abs(c1.seat_id -c2.seat_id) = 1 and (c1.free = 1 and c2.free = 1)
order by c1.seat_id;

输出如下

以上就是全部答案,如果对你有帮助请点个赞,谢谢。

来源:力扣(leecode

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 转载请注明出处:

603. 连续空余座位-优快云博客在 PostgreSQL 中,free存储为整数。请使用:boolean将其转换为布尔格式。我会尽快把力扣上的所有数据库题目发出来。该表的每一行表示第 i 个座位是否空闲。1 表示空闲,0 表示被占用。著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。测试用例的生成使得两个以上的座位连续可用。Seat_id 是该表的自动递增主键列。查找电影院所有连续可用的座位。结果表格式如下所示。https://blog.youkuaiyun.com/CYJ1844/article/details/144448834

我会尽快把力扣上的所有数据库题目发出来。感兴趣的可以点个赞与关注。每天不定时跟新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值