Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0000-0099/0016.3Sum Closest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

**方法一:排序 + 双指针**

将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。
我们将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。

时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。

Expand Down
11 changes: 5 additions & 6 deletions solution/0500-0599/0585.Investments in 2016/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,16 @@ tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一
```sql
# Write your MySQL query statement below
WITH
t AS (
T AS (
SELECT
tiv_2016,
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
FROM Insurance
)
SELECT
round(sum(TIV_2016), 2) AS tiv_2016
FROM t
WHERE cnt1 != 1 AND cnt2 = 1;
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
FROM T
WHERE cnt1 > 1 AND cnt2 = 1;
```

<!-- tabs:end -->
11 changes: 5 additions & 6 deletions solution/0500-0599/0585.Investments in 2016/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ So, the result is the sum of tiv_2016 of the first and last record, which is 45.
```sql
# Write your MySQL query statement below
WITH
t AS (
T AS (
SELECT
tiv_2016,
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
FROM Insurance
)
SELECT
round(sum(TIV_2016), 2) AS tiv_2016
FROM t
WHERE cnt1 != 1 AND cnt2 = 1;
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
FROM T
WHERE cnt1 > 1 AND cnt2 = 1;
```

<!-- tabs:end -->
11 changes: 5 additions & 6 deletions solution/0500-0599/0585.Investments in 2016/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Write your MySQL query statement below
WITH
t AS (
T AS (
SELECT
tiv_2016,
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
FROM Insurance
)
SELECT
round(sum(TIV_2016), 2) AS tiv_2016
FROM t
WHERE cnt1 != 1 AND cnt2 = 1;
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
FROM T
WHERE cnt1 > 1 AND cnt2 = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ ORDER BY count(1) DESC
LIMIT 1;
```

SQL Server

```sql
/* Write your T-SQL query statement below */
SELECT TOP 1
customer_number
FROM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ So the result is customer_number 3.

```sql
# Write your MySQL query statement below
SELECT
customer_number
FROM orders
GROUP BY customer_number
SELECT customer_number
FROM Orders
GROUP BY 1
ORDER BY count(1) DESC
LIMIT 1;
```

SQL Server

```sql
/* Write your T-SQL query statement below */
SELECT TOP 1
customer_number
FROM
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Write your MySQL query statement below
SELECT
customer_number
FROM orders
GROUP BY customer_number
SELECT customer_number
FROM Orders
GROUP BY 1
ORDER BY count(1) DESC
LIMIT 1;
29 changes: 23 additions & 6 deletions solution/0500-0599/0595.Big Countries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,34 @@ World 表:

<!-- 这里可写通用的实现逻辑 -->

**方法一:使用 WHERE + OR**

我们可以使用 `WHERE` + `OR` 查询出所有符合条件的国家。

**方法二:使用 UNION**

我们可以查询出所有面积大于等于 300 万平方公里的国家,然后再查询出所有人口大于等于 2500 万的国家,最后使用 `UNION` 将两个结果集合并起来。

<!-- tabs:start -->

### **SQL**

```sql
SELECT
name,
population,
area
FROM world
WHERE area > 3000000 OR population > 25000000;
# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;
```

```sql
# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area >= 3000000
UNION
SELECT name, population, area
FROM World
WHERE population >= 25000000;
```

<!-- tabs:end -->
21 changes: 15 additions & 6 deletions solution/0500-0599/0595.Big Countries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,21 @@ World table:
### **SQL**

```sql
SELECT
name,
population,
area
FROM world
WHERE area > 3000000 OR population > 25000000;
# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;
```

```sql
# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area >= 3000000
UNION
SELECT name, population, area
FROM World
WHERE population >= 25000000;
```

<!-- tabs:end -->
10 changes: 4 additions & 6 deletions solution/0500-0599/0595.Big Countries/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT
name,
population,
area
FROM world
WHERE area > 3000000 OR population > 25000000;
# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ Courses table:
### **SQL**

```sql
SELECT
class
FROM courses
# Write your MySQL query statement below
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(class) >= 5;
HAVING count(1) >= 5;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ Courses table:
### **SQL**

```sql
SELECT
class
FROM courses
# Write your MySQL query statement below
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(class) >= 5;
HAVING count(1) >= 5;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
class
FROM courses
# Write your MySQL query statement below
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(class) >= 5;
HAVING count(1) >= 5;
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ RequestAccepted 表:
### **SQL**

```sql
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(
round(
ifnull(
(
SELECT COUNT(DISTINCT requester_id, accepter_id)
SELECT count(DISTINCT requester_id, accepter_id)
FROM RequestAccepted
) / (
SELECT COUNT(DISTINCT sender_id, send_to_id)
FROM FriendRequest
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
),
2
0
),
0.00
2
) AS accept_rate;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ There are 4 unique accepted requests, and there are 5 requests in total. So the
### **SQL**

```sql
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(
round(
ifnull(
(
SELECT COUNT(DISTINCT requester_id, accepter_id)
SELECT count(DISTINCT requester_id, accepter_id)
FROM RequestAccepted
) / (
SELECT COUNT(DISTINCT sender_id, send_to_id)
FROM FriendRequest
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
),
2
0
),
0.00
2
) AS accept_rate;
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(
round(
ifnull(
(
SELECT COUNT(DISTINCT requester_id, accepter_id)
SELECT count(DISTINCT requester_id, accepter_id)
FROM RequestAccepted
) / (
SELECT COUNT(DISTINCT sender_id, send_to_id)
FROM FriendRequest
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
),
2
0
),
0.00
2
) AS accept_rate;