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

相关推荐
阳艳讲ai7 分钟前
中小企业AI技术方案选型评估框架:四类架构与评估指标分析
大数据·人工智能·企业ai培训·ai获客·九尾狐ai·ai应用工具
易观Analysys12 分钟前
中美Agent生态的路径差异——《重构与崛起——OpenClaw时代的中国Agent产业生态报告》解读三
大数据·人工智能
信竞星球_少儿编程题库17 分钟前
2026年全国信息素养大赛算法应用主题赛 丝路新城 Python 模拟卷(三)
开发语言·python·算法
zhishijike17 分钟前
全国行政区划sql(省市区)
数据库·sql·mysql
早川91923 分钟前
Hbase、MySQL和Redis区别
redis·mysql·hbase
云泽80825 分钟前
笔试算法 - 滑动窗口篇(二):从异位词到最小覆盖子串的通用框架
c++·算法
qq_2965532729 分钟前
[特殊字符] 搜索插入位置:从O(n)到O(log n)的优雅进化
数据结构·算法·面试·分类·柔性数组
凯瑟琳.奥古斯特34 分钟前
力扣3654:二维矩阵连续空位统计
数据结构·数据库·算法·职场和发展
再战300年43 分钟前
通过docker实现mysql一主多从
mysql·docker·容器