【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 ...
随机推荐
- 【MM系列】SAP S/4 HANA 1511的BP角色创建及供应商数据的创建方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP S/4 HANA 1511的 ...
- unity中的常遇到的问题
1.使用unity的MovieTexture播放视频在物体上,对象只能在电脑上 2.移动端播放全屏视频 Handheld.PlayFullScreenMovie(),视频文件必须放置在Streamin ...
- Logistic回归基础篇之梯度上升算法
代码示例: import numpy as np import matplotlib.pyplot as plt def loadDataSet(): dataMat = [];labelMat = ...
- Kubernetes网络隔离NetworkPolicy
Kubernetes要求集群中所有pod,无论是节点内还是跨节点,都可以直接通信,或者说所有pod工作在同一跨节点网络,此网络一般是二层虚拟网络,称为pod网络.在安装引导kubernetes时,由选 ...
- Mysql 字段类型与查询类型不一致导致索引使用失败
今天优化数据库的慢查询,有一条Sql让我百思不得其jie,就是他了. SELECT * FROM test WHERE user_id=1; 用explain 去分析一下 索引都有了,为什么还要扫描全 ...
- JDBC的一些简单通用代码
JDBC的一些简单通用代码 功能包括 连接数据库 查询操作 执行sql语句 jdbc相关类的加载 关闭连接 获取数据库格式的当前时间 代码 package dao; import java.sql.C ...
- 洛谷 P1879 玉米田Corn Fields 题解
题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...
- Exchange 2010的部署
实验拓扑: 实验准备条件:计算机基本配置的准备:DC (vbers):配置IP地址.子网掩码.网关EX (vbers2):配置IP地址.子网掩码.网关.指定DNS 域环境搭建的准备1.在计算机名为vb ...
- 解决sudo用户找不到环境变量的问题
出于安全方面的考虑,使用sudo执行命令将在一个最小化的环境中执行,环境变量都重置成默认状态.所以PATH这个变量不包括用户自定义设置的内容 在sudo用户的主目录里的.bashrc中添加如下内容即可 ...
- BZOJ 4141 [Thu Summer Camp 2013]魔塔
权限题qwq 以下设值域大小为\(m\) 先考虑枚举攻击力,因为首先攻击力决定每个怪物的攻击次数,然后对于每个怪物,攻击次数为\(\lceil\frac{hp_i}{ATK-def_i}\rceil\ ...