加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode626. 换座位 | Exchange Seats

发布时间:2020-12-14 05:03:13 所属栏目:百科 来源:网络整理
导读:SQL架构 1 Create table If Not Exists seat(id int ,student varchar( 255 )) 2 Truncate table seat 3 insert into seat (id,student) values ( ‘ 1 ‘ , ‘ Abbot ‘ ) 4 insert into seat (id,student) values ( ‘ 2 ‘ , ‘ Doris ‘ ) 5 insert into

SQL架构

1 Create table If Not Exists seat(id int,student varchar(255))
2 Truncate table seat
3 insert into seat (id,student) values (1,Abbot)
4 insert into seat (id,student) values (2,Doris)
5 insert into seat (id,student) values (3,Emerson)
6 insert into seat (id,student) values (4,Green)
7 insert into seat (id,student) values (5,Jeames)

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.


小美是一所中学的信息科技老师,她有一张?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  |
+---------+---------+

注意:

如果学生人数是奇数,则不需要改变最后一个同学的座位。


Runtime:?152 ms

 1 # Write your MySQL query statement below
 2 select 
 3     (case when id % 2 = 0 then id - 1
 4         when id % 2 = 1 and id <> c.cnt then id + 1
 5         else id
 6      end) as id,student
 7 from 
 8     seat, 9     (select count(id) as cnt from seat) as c
10 order by id

253ms

 1 # Write your MySQL query statement below
 2 SELECT 
 3     (CASE
 4         WHEN MOD(id,2) != 0 AND counts != id THEN id + 1
 5         WHEN MOD(id,2) != 0 AND counts = id THEN id
 6         ELSE id - 1
 7     END) as id, 8     student
 9 FROM 
10     seat,11     (SELECT COUNT(*) as counts
12     FROM seat) as seat_counts
13 ORDER BY id ASC;

255ms

1 # Write your MySQL query statement below
2 select * from (
3 select case when b.Id is null then a.Id else b.Id end as Id,a.student from seat a left join seat b on a.Id+1=b.Id where mod(a.id,2)=1
4 union 
5 select b.Id,a.student from seat a left join seat b on a.Id-1=b.Id where mod(a.id,2)=0
6 ) a order by a.id

257ms

1 select
2     case
3         when id%2=1 and id=(select max(id) from seat) then id
4         when id%2=1 then id+1
5         else id-1 end as id,6     student
7 FROM seat
8 order by id

258ms

1 # Write your MySQL query statement below
2 select case 
3     when mod(id,2) =1 and id = (select max(id) as sid from seat)  then id  
4     when mod(id,2) =1 then id+1 
5     else id-1 end as id,student
6 from seat
7 order by id

260ms

1 SELECT
2     (CASE WHEN id%2=0 THEN id-1 
3      WHEN id%2=1 AND id<(select max(id) FROM seat) THEN id+1 
4     ELSE id
5     END) as id
6 ,student
7 FROM seat
8 ORDER BY id;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读