626. Exchange Seats-(LeetCode之Database篇)
问题表述
数据库表如下:
| id | student |
|---|---|
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
现在要通过SQL语句将表变换成如下:
| id | student |
|---|---|
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
即id不变,奇数位和偶数位交换位置,如果表的总行数为奇数,则最后一行不变。
问题解决
首先看到这个问题,我就想SQL里面是不是有什么置换函数之类的,结果去查了查,并没有这样的函数。在我尝试了各种select方法后,还是没能将这题解出来…最后还是去讨论区看了看大神们的解答,看完各种答案后瞬间豁然开朗。
这题的解题思路其实并不是想办法将student的列置换,而是通过操作id列来达到置换的效果。
比较通过的解法就是下面这种:
select
if(id < (select count(*) from seat), if(id mod 2=0, id-1, id+1), if(id mod 2=0, id-1, id)) as id, student
from seat
order by id asc
其中IF函数的用法:
格式:IF(Condition,A,B)
意义:当Condition为TRUE时,返回A;当Condition为FALSE时,返回B。
所以上面的SQL语句就是,先对id进行操作。先计算总行数,最后一行如果是奇数id不变,如果是偶数id减1,其余行id为奇数的让id加1,id为偶数的让id减1,最后再对id做升序操作,就可以得到结果了。
其中还有一种解法:
/* get all the even numbered rows as odd numbered rows */
SELECT s1.id - 1 as id, s1.student
FROM Seat s1
WHERE s1.id MOD 2 = 0
UNION
/* get all the odd numbered rows as even numbered rows */
SELECT s2.id + 1 as id, s2.student
FROM Seat s2
WHERE s2.id MOD 2 = 1 AND s2.id != (SELECT MAX(id) FROM Seat)
/* Just don't get the last row as we will handle it in the next UNION */
UNION
/* get the last row if odd and don't change the id value */
SELECT s3.id, s3.student
FROM Seat s3
WHERE s3.id MOD 2 = 1 AND s3.id = (SELECT MAX(id) FROM Seat)
/* Order the result by id */
ORDER BY id ASC;
思路都和第一种方法大同小异。
626. Exchange Seats-(LeetCode之Database篇)的更多相关文章
- LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- 搭建IIS CA DC Exchange TMG SQL (CA DC篇)
搭建IIS CA DC Exchange TMG SQL (CA DC篇) 步骤 1: 在“下一步(N) > (按下按钮)”(位于“添加角色向导”中)上用户左键单击 步骤 2: 在“Ac ...
- 【leetcode】Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- [SQL]LeetCode626. 换座位 | Exchange Seats
SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...
- c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- 【持续更新】leetcode算法-数组篇
会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助. 1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...
- leetcode中Database题(一)
Combine Two Tables Table: Person +-------------+---------+ | Column Name | Type | +-------------+--- ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
随机推荐
- numpy 辨异(三)—— hstack/column_stack,linalg.eig/linalg.eigh
1. np.hstack np.column_stack >>> np.hstack([np.array([1, 2, 3]), np.array([4, 5, 6])]) arra ...
- solr+ Eclipse 4.3+ tomcat 7.5 +winds7(一)
这种方法是我自己依据对tomcat运行项目流程和solr的运行流程来自己弄的,所以有点麻烦,请到原地址查看心血谢谢:http://blog.csdn.net/chunlei_zhang/article ...
- xshell登陆Win10 Linux子系统
原文:xshell登陆Win10 Linux子系统 版权声明:转载请注明出处 https://blog.csdn.net/anychenp/article/details/78922320 修改端口 ...
- Enabling granular discretionary access control for data stored in a cloud computing environment
Enabling discretionary data access control in a cloud computing environment can begin with the obtai ...
- P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!
如图,为什么这样取出来的p,p1的值不一样呢? 165232328群友庾伟洪告诉我: P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址) ,你想P指向真正的对 ...
- Android 混淆代码汇总
为了防止别人对自己被盗的劳动,混淆代码可以被反编译可以有效地防止,以下在下面的代码混乱总结的步骤: 1. 大家可能已经注意到一个新的项目将在下面看到的物品都有这个proguard-project.tx ...
- WPF控件获得焦点时去除虚线框
原文:WPF控件获得焦点时去除虚线框 <Setter Property="FocusVisualStyle" Value="{x:Null}" />
- 基于IdentityServer4的单点登录——项目基本结构与流程
组成 IdentityServer,Api和Client(客户端,asp .net core)本文以官方demo:https://github.com/IdentityServer/IdentityS ...
- XML Serialize/Deserialize
using System; using System.Collections.Generic; using System.Globalization; using System.IO; using S ...
- 【全面解禁!真正的Expression Blend实战开发技巧】第一章 真正的开发中的最佳的做法
原文:[全面解禁!真正的Expression Blend实战开发技巧]第一章 真正的开发中的最佳的做法 从设计者到开发者 设计师创建一个应用程序的布局然后让开发者去实现. 从开发者到设计者 开发者创建 ...