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

相关推荐
pearbing5 分钟前
天猫UV量提高实用指南:找准方向,稳步突破流量瓶颈
大数据·uv·天猫uv量提高·天猫uv量·uv量提高·天猫提高uv量
Dxy123931021610 分钟前
MySQL如何高效查询表数据量:从基础到进阶的优化指南
数据库·mysql
Dying.Light13 分钟前
MySQL相关问题
数据库·mysql
源代码•宸24 分钟前
大厂技术岗面试之谈薪资
经验分享·后端·面试·职场和发展·golang·大厂·职级水平的薪资
马猴烧酒.1 小时前
【面试八股|JVM虚拟机】JVM虚拟机常考面试题详解
jvm·面试·职场和发展
Dxy12393102161 小时前
Elasticsearch 索引与映射:为你的数据打造一个“智能仓库”
大数据·elasticsearch·搜索引擎
Re.不晚1 小时前
MySQL进阶之战——索引、事务与锁、高可用架构的三重奏
数据库·mysql·架构
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
大闲在人2 小时前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
小熳芋2 小时前
443. 压缩字符串-python-双指针
算法