【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 ...
随机推荐
- [译]深入 NGINX: 为性能和扩展所做之设计
来自:http://ifeve.com/inside-nginx-how-we-designed-for-performance-scale/ 这篇文章写了nginx的设计,写的很仔细全面, 同时里面 ...
- Tensorflow 对上一节神经网络模型的优化
本节涉及的知识点: 1.在程序中查看变量的取值 2.张量 3.用张量重新组织输入数据 4.简化的神经网络模型 5.标量.多维数组 6.在TensorFlow中查看和设定张量的形态 7.用softmax ...
- Unity 动画属性
在动画的使用上使用不当的设置往往会造成不可预料的结果. 首先,如果动画自身可以驱动物体移动,那么在Animator组件上必须选择apply root motion,物体的动画位移才能生效,否则动画只能 ...
- 华为HCNA乱学Round 2:路由基础
- form 源码刨析
def clean_name(self) value = self.cleaned_data.get('name') if "金-瓶-梅" not in value: raise ...
- form 校验
import refrom django.forms import Formfrom django.forms import widgetsfrom django.forms import field ...
- [JS] 点击按钮触发后台事件前,弹出确认框
只需要在button中设置onclick属性触发事件即可 下面以ASP.NET代码为例, ASP.NET中按钮客户端触发js代码的属性是OnClientClick <asp:Button ID= ...
- python 并发编程 IO模型介绍
gevent 底层是怎么实现? io模型4个重要概念: 两类 一类:同步.异步 提交任务的方式 同步: 提交完任务后,在原地等待结果,拿到结果后,才执行下一行代码 #所谓同步,就是在发出一个功能调用时 ...
- PTA(Basic Level)1010.一元多项式求导
设计函数求一元多项式的导数.(注:\(x^n\)(\(n\)为整数)的一阶导数为\(nx^{n−1}\).) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数) ...
- Mybatis(一) 入门
对原生态jdbc程序中问题总结 创建mysql数据库 jdbc程序 使用jdbc查询mysql数据库中用户表的记录. 创建java工程,加入jar包 数据库驱动包 第一个是mysql驱动 第二个是or ...