力扣之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 结果截图

相关推荐
爱思德学术3 分钟前
【SPIE出版】黄冈师范学院主办!第四届大数据、计算智能与应用国际会议(BDCIA 2026)
大数据·算法·数据分析·云计算·etl
洛水水3 分钟前
【力扣100题】40.二叉树中的最大路径和
算法·leetcode·深度优先
Unbelievabletobe6 分钟前
免费外汇api的响应时间在不同时段下的波动分析
大数据·开发语言·前端·python
跨境卫士—小依6 分钟前
美国邮政渠道开始计税后跨境卖家如何重写小包报价逻辑
大数据·人工智能·安全·跨境电商·营销策略
洛水水7 分钟前
【力扣100题】37.从前序与中序遍历序列构造二叉树
c++·算法·leetcode
zyq99101_19 分钟前
递归与动态规划实战代码解析
python·算法·蓝桥杯
阿苟15 分钟前
数据库重点难点
redis·后端·mysql
一只机电自动化菜鸟18 分钟前
一建机电备考笔记(34)焊接技术(设备与材料1)(含考频+题型)
笔记·学习·职场和发展·生活·学习方法
敖正炀23 分钟前
MySQL 线上问题实战演练:复合故障排查与系统设计
mysql
醇氧30 分钟前
CentOS 7安装 mysql-8.0.27-1.el7.x86_64.rpm 安装包
android·mysql·centos