Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.0196,1502,1790,1822,1873
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
YangFong committed Apr 6, 2022
1 parent d64b900 commit e362c77
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 35 deletions.
31 changes: 18 additions & 13 deletions solution/0100-0199/0196.Delete Duplicate Emails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,27 @@ Person 表:
### **SQL**

```sql
delete from Person
where Id not in (
select min(Id)
from (
select *
from Person
) as p
group by p.Email
)
DELETE
FROM
Person
WHERE
Id NOT IN (
SELECT
MIN( Id )
FROM
( SELECT * FROM Person ) AS p
GROUP BY
p.Email
);
```

```sql
# Write your MySQL query statement below
DELETE p1
FROM Person p1, Person p2
WHERE p1.email = p2.email and p1.id > p2.id
DELETE p2
FROM
person AS p1
JOIN person AS p2 ON p1.email = p2.email
WHERE
p1.id < p2.id;
```

<!-- tabs:end -->
31 changes: 18 additions & 13 deletions solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,27 @@ Person table:
### **SQL**

```sql
delete from Person
where Id not in (
select min(Id)
from (
select *
from Person
) as p
group by p.Email
)
DELETE
FROM
Person
WHERE
Id NOT IN (
SELECT
MIN( Id )
FROM
( SELECT * FROM Person ) AS p
GROUP BY
p.Email
);
```

```sql
# Write your MySQL query statement below
DELETE p1
FROM Person p1, Person p2
WHERE p1.email = p2.email and p1.id > p2.id
DELETE p2
FROM
person AS p1
JOIN person AS p2 ON p1.email = p2.email
WHERE
p1.id < p2.id;
```

<!-- tabs:end -->
21 changes: 12 additions & 9 deletions solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
delete from Person
where Id not in (
select min(Id)
from (
select *
from Person
) as p
group by p.Email
)
DELETE
FROM
Person
WHERE
Id NOT IN (
SELECT
MIN( Id )
FROM
( SELECT * FROM Person ) AS p
GROUP BY
p.Email
);
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ var canMakeArithmeticProgression = function (arr) {
};
```

### **Rust**

```rust
impl Solution {
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
arr.sort();
let n = arr.len();
let target = arr[0] - arr[1];
for i in 2..n {
if arr[i - 1] - arr[i] != target {
return false;
}
}
true
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ var canMakeArithmeticProgression = function (arr) {
};
```

### **Rust**

```rust
impl Solution {
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
arr.sort();
let n = arr.len();
let target = arr[0] - arr[1];
for i in 2..n {
if arr[i - 1] - arr[i] != target {
return false;
}
}
true
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
arr.sort();
let n = arr.len();
let target = arr[0] - arr[1];
for i in 2..n {
if arr[i - 1] - arr[i] != target {
return false;
}
}
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ func areAlmostEqual(s1 string, s2 string) bool {
}
```

### **Rust**

```rust
impl Solution {
pub fn are_almost_equal(s1: String, s2: String) -> bool {
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
let n = s1.len();
let mut indexs = vec![];
for i in 0..n {
let (c1, c2) = (s1[i], s2[i]);
if c1 != c2 {
indexs.push(i);
if indexs.len() > 2 {
return false;
}
}
}
let size = indexs.len();
if size == 2 {
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
}
size != 1
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ func areAlmostEqual(s1 string, s2 string) bool {
}
```

### **Rust**

```rust
impl Solution {
pub fn are_almost_equal(s1: String, s2: String) -> bool {
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
let n = s1.len();
let mut indexs = vec![];
for i in 0..n {
let (c1, c2) = (s1[i], s2[i]);
if c1 != c2 {
indexs.push(i);
if indexs.len() > 2 {
return false;
}
}
}
let size = indexs.len();
if size == 2 {
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
}
size != 1
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn are_almost_equal(s1: String, s2: String) -> bool {
let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
let n = s1.len();
let mut indexs = vec![];
for i in 0..n {
let (c1, c2) = (s1[i], s2[i]);
if c1 != c2 {
indexs.push(i);
if indexs.len() > 2 {
return false;
}
}
}
let size = indexs.len();
if size == 2 {
return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
}
size != 1
}
}
21 changes: 21 additions & 0 deletions solution/1800-1899/1822.Sign of the Product of an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

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

不可模拟乘积过程,给定的范围有可能导致数值溢出,只关注数值的符号变化即可。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -146,6 +148,25 @@ func arraySign(nums []int) int {
}
```

### **Rust**

```rust
use std::cmp::Ordering;
impl Solution {
pub fn array_sign(nums: Vec<i32>) -> i32 {
let mut res = 1;
for num in nums.iter() {
match num.cmp(&0) {
Ordering::Equal => return 0,
Ordering::Less => res *= -1,
Ordering::Greater => {}
}
}
res
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ func arraySign(nums []int) int {
}
```

### **Rust**

```rust
use std::cmp::Ordering;
impl Solution {
pub fn array_sign(nums: Vec<i32>) -> i32 {
let mut res = 1;
for num in nums.iter() {
match num.cmp(&0) {
Ordering::Equal => return 0,
Ordering::Less => res *= -1,
Ordering::Greater => {}
}
}
res
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::cmp::Ordering;
impl Solution {
pub fn array_sign(nums: Vec<i32>) -> i32 {
let mut res = 1;
for num in nums.iter() {
match num.cmp(&0) {
Ordering::Equal => return 0,
Ordering::Less => res *= -1,
Ordering::Greater => {}
}
}
res
}
}
23 changes: 23 additions & 0 deletions solution/1800-1899/1873.Calculate Special Bonus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,30 @@ employee_id 是这个表的主键。
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql
SELECT
employee_id,
CASE
WHEN employee_id % 2 = 0
OR LEFT(name, 1) = 'M' THEN 0
ELSE salary
END AS bonus
FROM
employees;
```

MySQL

```sql
SELECT
employee_id,
IF(
employee_id % 2 = 0
OR LEFT(name, 1) = 'M',
0,
salary
) AS bonus
FROM
employees;
```

<!-- tabs:end -->
23 changes: 23 additions & 0 deletions solution/1800-1899/1873.Calculate Special Bonus/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,30 @@ The rest of the employees get a 100% bonus.
### **SQL**

```sql
SELECT
employee_id,
CASE
WHEN employee_id % 2 = 0
OR LEFT(name, 1) = 'M' THEN 0
ELSE salary
END AS bonus
FROM
employees;
```

MySQL

```sql
SELECT
employee_id,
IF(
employee_id % 2 = 0
OR LEFT(name, 1) = 'M',
0,
salary
) AS bonus
FROM
employees;
```

<!-- tabs:end -->
Loading

0 comments on commit e362c77

Please sign in to comment.