Skip to content

Commit e362c77

Browse files
committed
feat: add solutions to lc problems: No.0196,1502,1790,1822,1873
- No.0196.Delete Duplicate Emails - No.1502.Can Make Arithmetic Progression From Sequence - No.1790.Check if One String Swap Can Make Strings Equal - No.1822.Sign of the Product of an Array - No.1873.Calculate Special Bonus
1 parent d64b900 commit e362c77

File tree

15 files changed

+279
-35
lines changed

15 files changed

+279
-35
lines changed

solution/0100-0199/0196.Delete Duplicate Emails/README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,27 @@ Person 表:
5959
### **SQL**
6060

6161
```sql
62-
delete from Person
63-
where Id not in (
64-
select min(Id)
65-
from (
66-
select *
67-
from Person
68-
) as p
69-
group by p.Email
70-
)
62+
DELETE
63+
FROM
64+
Person
65+
WHERE
66+
Id NOT IN (
67+
SELECT
68+
MIN( Id )
69+
FROM
70+
( SELECT * FROM Person ) AS p
71+
GROUP BY
72+
p.Email
73+
);
7174
```
7275

7376
```sql
74-
# Write your MySQL query statement below
75-
DELETE p1
76-
FROM Person p1, Person p2
77-
WHERE p1.email = p2.email and p1.id > p2.id
77+
DELETE p2
78+
FROM
79+
person AS p1
80+
JOIN person AS p2 ON p1.email = p2.email
81+
WHERE
82+
p1.id < p2.id;
7883
```
7984

8085
<!-- tabs:end -->

solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,27 @@ Person table:
5555
### **SQL**
5656

5757
```sql
58-
delete from Person
59-
where Id not in (
60-
select min(Id)
61-
from (
62-
select *
63-
from Person
64-
) as p
65-
group by p.Email
66-
)
58+
DELETE
59+
FROM
60+
Person
61+
WHERE
62+
Id NOT IN (
63+
SELECT
64+
MIN( Id )
65+
FROM
66+
( SELECT * FROM Person ) AS p
67+
GROUP BY
68+
p.Email
69+
);
6770
```
6871

6972
```sql
70-
# Write your MySQL query statement below
71-
DELETE p1
72-
FROM Person p1, Person p2
73-
WHERE p1.email = p2.email and p1.id > p2.id
73+
DELETE p2
74+
FROM
75+
person AS p1
76+
JOIN person AS p2 ON p1.email = p2.email
77+
WHERE
78+
p1.id < p2.id;
7479
```
7580

7681
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
delete from Person
2-
where Id not in (
3-
select min(Id)
4-
from (
5-
select *
6-
from Person
7-
) as p
8-
group by p.Email
9-
)
1+
DELETE
2+
FROM
3+
Person
4+
WHERE
5+
Id NOT IN (
6+
SELECT
7+
MIN( Id )
8+
FROM
9+
( SELECT * FROM Person ) AS p
10+
GROUP BY
11+
p.Email
12+
);

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ var canMakeArithmeticProgression = function (arr) {
9191
};
9292
```
9393

94+
### **Rust**
95+
96+
```rust
97+
impl Solution {
98+
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
99+
arr.sort();
100+
let n = arr.len();
101+
let target = arr[0] - arr[1];
102+
for i in 2..n {
103+
if arr[i - 1] - arr[i] != target {
104+
return false;
105+
}
106+
}
107+
true
108+
}
109+
}
110+
```
111+
94112
### **...**
95113

96114
```

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ var canMakeArithmeticProgression = function (arr) {
8181
};
8282
```
8383

84+
### **Rust**
85+
86+
```rust
87+
impl Solution {
88+
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
89+
arr.sort();
90+
let n = arr.len();
91+
let target = arr[0] - arr[1];
92+
for i in 2..n {
93+
if arr[i - 1] - arr[i] != target {
94+
return false;
95+
}
96+
}
97+
true
98+
}
99+
}
100+
```
101+
84102
### **...**
85103

86104
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
3+
arr.sort();
4+
let n = arr.len();
5+
let target = arr[0] - arr[1];
6+
for i in 2..n {
7+
if arr[i - 1] - arr[i] != target {
8+
return false;
9+
}
10+
}
11+
true
12+
}
13+
}

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ func areAlmostEqual(s1 string, s2 string) bool {
143143
}
144144
```
145145

146+
### **Rust**
147+
148+
```rust
149+
impl Solution {
150+
pub fn are_almost_equal(s1: String, s2: String) -> bool {
151+
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
152+
let n = s1.len();
153+
let mut indexs = vec![];
154+
for i in 0..n {
155+
let (c1, c2) = (s1[i], s2[i]);
156+
if c1 != c2 {
157+
indexs.push(i);
158+
if indexs.len() > 2 {
159+
return false;
160+
}
161+
}
162+
}
163+
let size = indexs.len();
164+
if size == 2 {
165+
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
166+
}
167+
size != 1
168+
}
169+
}
170+
```
171+
146172
### **...**
147173

148174
```

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,32 @@ func areAlmostEqual(s1 string, s2 string) bool {
130130
}
131131
```
132132

133+
### **Rust**
134+
135+
```rust
136+
impl Solution {
137+
pub fn are_almost_equal(s1: String, s2: String) -> bool {
138+
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
139+
let n = s1.len();
140+
let mut indexs = vec![];
141+
for i in 0..n {
142+
let (c1, c2) = (s1[i], s2[i]);
143+
if c1 != c2 {
144+
indexs.push(i);
145+
if indexs.len() > 2 {
146+
return false;
147+
}
148+
}
149+
}
150+
let size = indexs.len();
151+
if size == 2 {
152+
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
153+
}
154+
size != 1
155+
}
156+
}
157+
```
158+
133159
### **...**
134160

135161
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn are_almost_equal(s1: String, s2: String) -> bool {
3+
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
4+
let n = s1.len();
5+
let mut indexs = vec![];
6+
for i in 0..n {
7+
let (c1, c2) = (s1[i], s2[i]);
8+
if c1 != c2 {
9+
indexs.push(i);
10+
if indexs.len() > 2 {
11+
return false;
12+
}
13+
}
14+
}
15+
let size = indexs.len();
16+
if size == 2 {
17+
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
18+
}
19+
size != 1
20+
}
21+
}

solution/1800-1899/1822.Sign of the Product of an Array/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

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

60+
不可模拟乘积过程,给定的范围有可能导致数值溢出,只关注数值的符号变化即可。
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
@@ -146,6 +148,25 @@ func arraySign(nums []int) int {
146148
}
147149
```
148150

151+
### **Rust**
152+
153+
```rust
154+
use std::cmp::Ordering;
155+
impl Solution {
156+
pub fn array_sign(nums: Vec<i32>) -> i32 {
157+
let mut res = 1;
158+
for num in nums.iter() {
159+
match num.cmp(&0) {
160+
Ordering::Equal => return 0,
161+
Ordering::Less => res *= -1,
162+
Ordering::Greater => {}
163+
}
164+
}
165+
res
166+
}
167+
}
168+
```
169+
149170
### **...**
150171

151172
```

solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ func arraySign(nums []int) int {
136136
}
137137
```
138138

139+
### **Rust**
140+
141+
```rust
142+
use std::cmp::Ordering;
143+
impl Solution {
144+
pub fn array_sign(nums: Vec<i32>) -> i32 {
145+
let mut res = 1;
146+
for num in nums.iter() {
147+
match num.cmp(&0) {
148+
Ordering::Equal => return 0,
149+
Ordering::Less => res *= -1,
150+
Ordering::Greater => {}
151+
}
152+
}
153+
res
154+
}
155+
}
156+
```
157+
139158
### **...**
140159

141160
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::cmp::Ordering;
2+
impl Solution {
3+
pub fn array_sign(nums: Vec<i32>) -> i32 {
4+
let mut res = 1;
5+
for num in nums.iter() {
6+
match num.cmp(&0) {
7+
Ordering::Equal => return 0,
8+
Ordering::Less => res *= -1,
9+
Ordering::Greater => {}
10+
}
11+
}
12+
res
13+
}
14+
}

solution/1800-1899/1873.Calculate Special Bonus/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,30 @@ employee_id 是这个表的主键。
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```sql
72+
SELECT
73+
employee_id,
74+
CASE
75+
WHEN employee_id % 2 = 0
76+
OR LEFT(name, 1) = 'M' THEN 0
77+
ELSE salary
78+
END AS bonus
79+
FROM
80+
employees;
81+
```
82+
83+
MySQL
7284

85+
```sql
86+
SELECT
87+
employee_id,
88+
IF(
89+
employee_id % 2 = 0
90+
OR LEFT(name, 1) = 'M',
91+
0,
92+
salary
93+
) AS bonus
94+
FROM
95+
employees;
7396
```
7497

7598
<!-- tabs:end -->

solution/1800-1899/1873.Calculate Special Bonus/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,30 @@ The rest of the employees get a 100% bonus.
6464
### **SQL**
6565

6666
```sql
67+
SELECT
68+
employee_id,
69+
CASE
70+
WHEN employee_id % 2 = 0
71+
OR LEFT(name, 1) = 'M' THEN 0
72+
ELSE salary
73+
END AS bonus
74+
FROM
75+
employees;
76+
```
77+
78+
MySQL
6779

80+
```sql
81+
SELECT
82+
employee_id,
83+
IF(
84+
employee_id % 2 = 0
85+
OR LEFT(name, 1) = 'M',
86+
0,
87+
salary
88+
) AS bonus
89+
FROM
90+
employees;
6891
```
6992

7093
<!-- tabs:end -->

0 commit comments

Comments
 (0)