LeetCode:626.换座位
题目
小美是一所中学的信息科技老师,她有一张 seat
座位表,平时用来储存学生名字和与他们相对应的座位 id
。
其中纵列的 id
是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
示例:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
假如数据输入的是上表,则输出结果如下:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
解答
解法一
根据自己的逻辑尝试了一下,居然提交通过,哈哈!
---- MySQL ----
# Write your MySQL query statement below
select case when b.cnt % 2 = 0 then
case when a.id % 2 = 0 then id - 1
when a.id % 2 = 1 then id + 1 end
when b.cnt % 2 = 1 then
case when a.id < b.cnt then
case when a.id % 2 = 0 then id - 1
when a.id % 2 = 1 then id + 1 end
else id end
end as id,
student
from seat a,
(
select count(*) as cnt
from seat
) b
order by id; ---- 126ms
引入总行数计数 cnt
,作为另外一个表;
判断当前行数是偶数还是奇数,再判断当前 id
是奇数还是偶数,奇数+1,偶数 -1,依次类推;
当行数为奇数时,最后一行 id
不作任何操作。
简化一下。
---- MySQL ----
# Write your MySQL query statement below
select if(id % 2 = 0,
id - 1,
if(id = cnt,
id,
id + 1
)
) as id,
student
from
(
select count(*) as cnt from seat
) as a,
seat
order by id; ---- 133ms
另一种。
---- MySQL ----
# Write your MySQL query statement below
select (case when id % 2 = 0 then id -1
when id = (select max(id) from seat) then id
else id + 1 end) as id,
student
from seat
order by id; ---- 156ms
解法二
通过左连接的方法。
---- MySQL ----
# Write your MySQL query statement below
select a.id,
ifnull(b.student, a.student) as student
from seat a
left join seat b
on (a.id % 2 = 1 && a.id = b.id -1) || (a.id % 2 = 0 && a.id = b.id + 1)
order by a.id ---- 135ms
真的很天才,佩服!
解法三
通过异或的方法,更是牛逼!
0 ^ 1 = 1
1 ^ 1 = 0
---- MySQL ----
select b.id,
a.student
from seat a,
seat b,
(select count(*) as cnt from seat) c
where b.id = 1 ^ (a.id - 1) + 1 || (c.cnt % 2 && b.id = c.cnt && a.id = c.cnt); ---- 132ms
如果是一个偶数^1,结果是偶数+1;
如果是一个奇数^1,结果是奇数-1;
(id -1 ) ^ 1 + 1
可以把1、2交换位置,3、4交换位置。
思考
MySQL
中的 ifnull()
函数用于判断第一个表达式是否为 null
,如果为 null
则返回第二个表达式的值。
异或操作,真是神,往往能达到意想不到的效果,get!
LeetCode:626.换座位的更多相关文章
- Lc626_换座位
626. 换座位 SQL架构 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id. 其中纵列的 id 是连续递增的 小美想改变相邻俩学生的座位. ...
- mysql查询之上升的温度,有趣的电影,超过5名学生的课,大国,反转性别, 换座位
最近发现一个网站 力扣 查看 上面有很多算法和数据库的题目,做了一下,发现自己平时都疏忽了,因此边做边记录下来 1.上升的温度 给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天 ...
- [SQL]LeetCode626. 换座位 | Exchange Seats
SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...
- LeetCode.1217-交换芯片(Play with Chips)
这是小川的第次更新,第篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第270题(顺位题号是1217).There are some chips, and the i-th ch ...
- 【bzoj4720】【noip2016】【换座位】期望dp+Floyd
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62370736 wa...已经快一年了,重新来做这 ...
- Java实现 LeetCode 1227 飞机座位分配概率
1227. 飞机座位分配概率 有 n 位乘客即将登机,飞机正好有 n 个座位.第一位乘客的票丢了,他随便选了一个座位坐下. 剩下的乘客将会: 如果他们自己的座位还空着,就坐到自己的座位上, 当他们自己 ...
- 力扣Leetcode 1518. 换酒问题
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒.你购入了 numBottles 瓶酒. 如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的. 请你计算 最多 能喝到多少瓶酒. 示例: ...
- LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- CV1——学习笔记
计算机视觉(computer vision)是从图像和视频中提出数值或符号信息的计算系统,更形象一点说,计算机视觉是让计算机具备像人类一样的眼睛,看到图像,并理解图像. 计算机视觉三大应用:识别.检测 ...
- Jupyter Notebook 远程连接配置(转载)
转载博客的Jupyter Notebook远程连接配置方法. 0 - 参考资料 https://www.jianshu.com/p/08f276d48669?utm_campaign=maleskin ...
- C#依赖注入实例
http://qing.weibo.com/tj/400082fa33001h7x.html 1.5 实现依赖注入1.5.1 背景介绍 设计模式中,尤其是结构型模式很多时候解决的就是对象间的依赖关系, ...
- 005-多线程-集合-Map-ConcurrentSkipListMap
一.概述 ConcurrentSkipListMap是线程安全的有序的哈希表,适用于高并发的场景. ConcurrentSkipListMap和TreeMap,它们虽然都是有序的哈希表.但是,第一,它 ...
- docker之redis使用
#拉取redis > docker pull redis:latest latest: Pulling from library/redis 8d691f585fa8: Pull complet ...
- ContextCleaner ——Spark 应用程序的垃圾回收器
ContextCleaner是一个Spark服务,负责在应用程序范围内清除 shuffles, RDDs, broadcasts, accumulators和checkpointed RDDs,目的是 ...
- matlab学习——04图与网络(最短路,最小生成树,最大流)
04图与网络 1.最短路 (1) 自己写的dijstra算法 format compact; clc,clear all a=zeros(6); a(1,2)=50;a(1,4)=40;a(1,5)= ...
- FastCGI模式编译安装LAMP+Xcache
PHP的工作模式:php在lamp环境下共有三种工作模式:CGI模式.apache模块.FastCGI模式.CGI模式下运行PHP,性能不是很好.(已淘汰)FastCGI的方式和apache模块的不同 ...
- Spring Cloud(7.2):配置Producer Server
我们首先创建一个生产者服务.这里以一个商品价格服务为例,这个微服务可以对商品-价格信息进行增删改查,当有商品-价格信息被更新或删除,则该微服务发送消息,告诉其他调用它的系统这条信息已经被修改. 配置p ...
- 车道线检测github集锦
re1. github_lane_detection; end