【leetcode】Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids. The column id is continuous increment.
Mary wants to change seats for the adjacent students.
Can you write a SQL query to output the result for Mary?
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
For the sample input, the output is:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
Note:
If the number of students is odd, there is no need to change the last one's seat.
解题思路:这个题目难度不大,第一眼看起来也许觉得有些繁琐,但是如果抽丝剥茧,将解题过程分解成以下几步,就会豁然开朗。
1.把所有奇数行的名字换成相邻的偶数行的名字。
select a.id,b.student from seat a left join seat b on a.id & 1 and a.id + 1 = b.id;
2.把所有偶数行的名字换成相邻的奇数行的名字。
select a.id,b.student from seat a left join seat b on a.id & 1 = 0 and a.id - 1 = b.id;
3.合并1和2的结果
select a.id,b.student from seat a left join seat b
on (case when a.id & 1 then a.id + 1 = b.id else a.id -1 = b.id end);
4.如果总行数为奇数,最后一行的名字不用替换。修正SQL如下:
select a.id,case when b.student is null then a.student else b.student end as student from seat a left join seat b
on (case when a.id & 1 then a.id + 1 = b.id else a.id -1 = b.id end);
【leetcode】Exchange Seats的更多相关文章
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- C++随笔(1)——关于C++11中的线程创建,join和detach
主要是和之前的博文有关,之前在这里有一部分代码是通过创建新的进程来应对新的用户请求的,但是基本没怎么解释怎么用的,所以这里做点小笔记. join 首先引入的库: #include <thread ...
- 【LeetCode】122、买卖股票的最佳时机 II
Best Time to Buy and Sell Stock II 题目等级:Easy 题目描述: Say you have an array for which the ith element i ...
- 【转载】ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
转载出处 在网上下载了一个免安装包的MySQL,准备自己create database jhp_test,使用的时候出现报错,如下: ERROR (): Access denied for user ...
- 线性模型-线性回归、Logistic分类
线性模型是机器学习中最简单的,最基础的模型结果,常常被应用于分类.回归等学习任务中. 回归和分类区别: 回归:预测值是一个连续的实数: 分类:预测值是离散的类别数据. 1. 线性模型做回归任务 ...
- python基础--面向对象之绑定非绑定方法
# 类中定义的函数分为两大类, #一,绑定方法(绑定给谁,谁来调用就自动将它本身当做第一个参数传入) # 1,绑定到类的方法:用classmethod装饰器装饰的方法. # 对象也可以掉用,仍将类作为 ...
- 将Lambda表达式作为参数传递并解析-在构造函数参数列表中使用Lambda表达式
public class DemoClass { /// <summary> /// 通过Lambda表达式,在构造函数中赋初始值 /// </summary> /// < ...
- 2019/12.09centos安装 | 无密钥登陆
centos配置 1.安装位置选择(我要配置分区) →完成 2.添加新挂载点:/boot 400M /swap 4GB / 期望容量空 3.设置root密码:字母+数字 4.重启 5.点击编辑,NA ...
- 重装java后hadoop配置文件的修改
1.删除hdfs-site.xml中dfs.namenode.name.dir目录和dfs.datanode.data.dir目录 然后 hdfs namenode -format 不然将无法启动na ...
- LOJ 10214 计算器 题解
题面 k==1时,快速幂就好了: k==2时,exgcd就好了,但要注意取模范围的控制: k==3时,BSGS可以解决高次同余方程: 然后就可以开心的A掉了,但要注意特殊情况的特判 #include ...
- 弹出ifream
top.$.jBox("iframe:"+'${ctx}/synopsis/hmlwxSynopsis/addItem', {title: "添加作品",w ...