Exchange Seats

H3C-V7防火墙配置SSL VPN IP资源典型案例(华三) 阅读详情

Exchange Seats

description:
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是连续相邻递增的。
题目让把相邻座位号的学生交换位置,也就是1和2换,3和4换。交换时直接交换学生的姓名。如果总人数是奇数的话,最后一名学生的位置不动。

这道题不好做,看了leetcode官方solution才懂了。要用到case when语句或者if函数。

思路:对于奇数的id,如果是最后一个id,则id不变;否则id=id+1;如果是偶数的id,则id=id-1。id号的总数可以用select Count(*) from seat查出来。

这是官方题解:

SELECT
    (CASE
        WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1
        WHEN MOD(id, 2) != 0 AND counts = id THEN id
        ELSE id - 1
    END) AS id,
    student
FROM
    seat,
    (SELECT
        COUNT(*) AS counts
    FROM
        seat) AS seat_counts
ORDER BY id ASC;

也可以用IF函数,IF(condition, value_if_true, value_if_false)
IF函数类似高级语言的三元表达式,condition为真时返回value_if_true,condition为假时返回value_if_false。
因此也可以这样写:

# Write your MySQL query statement below
select IF(id % 2 = 0, id - 1, 
         IF(id = (select count(*) from seat),
           id, id + 1))
as id, student from seat order by id;

谢谢你的观看!

Win10 / Win11 启用 Hyper-V 虚拟机详细教程(图文+命令行全攻略) 摘要:本文详细介绍Windows10/11系统启用Hyper-V虚拟化平台的方法,包含专业版/企业版的图形界面和PowerShell命令行操作,以及家庭版通过脚本强制启用的特殊方案。内容涵盖版本支持对比、操作步骤图解、常见问题解答和性能优化建议,帮助开发者快速搭建虚拟化环境进行多系统测试。附微软官方文档参考链接和详细操作示意图。 阅读详情

相关推荐

小白学习微信小程序的图像识别和文字识别技术

其中,type参数指定了识别的类型,imageUrl参数指定了图片的地址,success回调函数用于处理识别成功的结果,fail回调函数用于处理识别失败的情况。其中,type参数指定了识别的类型,imageUrl参数指定了图片的地址,success回调函数用于处理识别成功的结果,fail回调函数用于处理识别失败的情况。下面,我们来实现一个文字识别的应用案例,可以识别一张图片中的文字,并将识别结果显示到小程序的页面上。在小程序中,也可以使用微信提供的文字识别接口,实现文字识别的功能开发。

qq_67153941的博客 1643

626. 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...

betty1121的博客 809

Ubuntu20.04运行Vins-fusion

因已安装ROS noetic,eigen,opencv及ceres等库,这部分环境配置就不细讲,直接下载VINS-FUSION的代码库 下载编译 cd ~/vins-fusion/src git clone https://github.com/HKUST-Aerial-Robotics/VINS-Fusion.git cd ../ catkin_make source ~/catkin_ws/devel/setup.bash 编译时会发现一堆错误,别急,无非就是环境冲突问题,一个个解决 1)首先,ROS

xiaojinger_123的博客 1万+

[MySQL习题] 交换相邻id的座位 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 adja...

memory专栏 1070

从零开始用TS开发babylonjs - 本机环境搭建

babylonjs,初始化本地安装,web3d引擎

han3527638的专栏 793

wifi/BT芯片RTL8723DS驱动移植和测试之一wifi篇

本文主要介绍如何在stm32mp157中移植并测试芯片RTL8723DS的wifi功能。

汉尼拔勇闯天涯 8737

LeetCode //SQL - 626. Exchange Seats

【代码】LeetCode //SQL - 626. Exchange Seats。

Made in Code 706

注册GitHub时出现Unable to verify your captcha response.Please visit https://docs.github.com/解决方案(持续更新)

本文主要介绍了注册GitHub时出现Unable to verify your captcha response. Please visit https://docs.github.com/articles/troubleshooting-connectivity-problems/#troubleshooting-the-captcha for troubleshooting information.解决方案,希望能对学习使用GitHub的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 8万+

【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 ...

weixin_33692284的博客 191

190、LLC谐振变换器的样机调试实战(问题定位)

这篇实战笔记总结了LLC谐振变换器样机调试中的五大典型问题及解决方案:1)空载炸机源于谐振电容纹波电流能力不足;2)轻载啸叫因控制环路不稳定,需调整突发模式阈值;3)MOSFET过热由死区时间不当导致,需精确优化实现ZVS;4)输出纹波大由PCB布局耦合引起,需优化共模抑制;5)启动过冲需延长软启动时间。作者强调调试需结合示波器、热成像仪等工具,理论计算与实测并重,并详细记录每次参数调整。文章指出LLC调试是不断试错的过程,关键是通过现象分析本质原因。

Jack010169的博客 46

导数与微分在 FPS 射击游戏中的实际应用:把“手感”算出来

摘要:射击游戏开发中,单线程适用于简单小游戏(如单机打靶),所有任务由主线程顺序处理,代码简单但效率较低;多线程则适合复杂场景(如多人在线3D大作),通过渲染、逻辑、网络、音效等线程分工协作,确保画面流畅、响应迅速和实时同步。选择依据取决于游戏复杂度、联网需求和性能要求,小型游戏用单线程,大型高要求项目必须采用多线程架构。(149字)

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 3278

[LeetCode] 626. Exchange Seats

Mary is a teacher in a middle school and she has a tableseatstoring students' names and their corresponding seat ids. The columnidis continuous increment. Mary wants to change seats for the adjacent students. +---------+---------+ | id | stude...

Think 319

Valgrind 从入门到精通:内存调试和性能分析

Valgrind 提供了强大的 API,允许开发者编写自定义的检查工具。通过扩展 Valgrind,可以针对特定场景实现更精准的检测。

大模型大数据攻城狮的专栏 1215

Leetcode - 626. Exchange Seats

Mary is a teacher in a middle school and she has a tableseatstoring students' names and their corresponding seat ids. The columnidis continuous increment. Mary wants to change seats for the adja...

DaisyHHH的博客 327

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【leetcode】626. 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 ...

kangbin825的专栏 2669

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode 626. Exchange Seats

背景:seat表(id, student),其中id是递增的,交换相邻位置,如果是奇数个,最后一个不处理思路:交换idsql语句如下:select case when mod(id, 2) != 0 and cnt != id then id + 1 when mod(id, 2) != 0 and cnt = id then id else id - 1 end a...

多看多听多总结 459

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode:626. Exchange Seats

链接: https://leetcode.com/problems/exchange-seats/description/ 题目: Mary is a teacher in a middle school and she has a table seat storing students’ names and their corresponding seat ids. The column...

SCUTJcfeng的博客 1219

【sql】交换位置 Exchange Seats

为什么80%的码农都做不了架构师?>>> ...

weixin_34278190的博客 236

Leetcode | Database | Exchange Seats

题目描述 给出下面一个seat表,写出一个sql查询实现交换相邻学生的座位,如果学生人数为奇数最后一个学生的座位不变动。例如下表及其得出的相应结果: 得到的查询结果: 题目解答: 分析:对照上表及其查询结果可以得知,当原id为奇数时,交换座位后的id变为id+1,当原id为偶数时,交换座位后的id变为id-1,另一个方面需要考虑的是,学生人数为奇数时,最后一个学生的id不变,故应当用

qq_33186949的博客 1057

[SQL]LeetCode626. 换座位 | Exchange Seats

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30916125的博客 162
上一篇: Rising Temperature
下一篇: Reverse Linked List
_dingzhen
博客等级 码龄9年 120粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值