8000 solve problem Exchange Seats · P-ppc/leetcode@e1e4783 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1e4783

Browse files
committed
solve problem Exchange Seats
1 parent ffaab49 commit e1e4783

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,5 @@ All solutions will be accepted!
331331
|183|[Customers Who Never Order](https://leetcode-cn.com/problems/customers-who-never-order/description/)|[mysql](./database/CustomersWhoNeverOrder)|Easy|
332332
|197|[Rising Temperature](https://leetcode-cn.com/problems/rising-temperature/description/)|[mysql](./database/RisingTemperature)|Easy|
333333
|181|[Employees Earning More Than Their Managers](https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/description/)|[mysql](./database/EmployeesEarningMoreThanTheirManagers)|Easy|
334-
|176|[Second Highest Salary](https://leetcode-cn.com/problems/second-highest-salary/description/)|[mysql](./database/SecondHighestSalary)|Easy|
334+
|176|[Second Highest Salary](https://leetcode-cn.com/problems/second-highest-salary/description/)|[mysql](./database/SecondHighestSalary)|Easy|
335+
|626|[Exchange Seats](https://leetcode-cn.com/problems/exchange-seats/description/)|[mysql](./database/ExchangeSeats)|Medium|

database/ExchangeSeats/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Exchange Seats
2+
We can solve problem this problem by CASE function, or use union to solve it like below:
3+
```sql
4+
# Write your MySQL query statement below
5+
select id, student from (
6+
select id - 1 as id, student from seat where mod(id, 2) = 0 union
7+
select id + 1 as id, student from seat where mod(id, 2) = 1 and id != (select count(*) from seat) union
8+
select id, student from seat where mod(id, 2) = 1 and id = (select count(*) from seat)
9+
) as s order by id
10+
```

database/ExchangeSeats/solution.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
select (
3+
case
4+
when mod(id, 2) = 1 and id = counts then id
5+
when mod(id, 2) = 1 and id < counts then id + 1
6+
else id - 1
7+
end) as id, student from seat, (select count(*) as counts from seat) as seat_counts order by id

0 commit comments

Comments
 (0)
0