From e362c7701bccfc4822c354db84b4fde4e11c2f29 Mon Sep 17 00:00:00 2001 From: YangFong Date: Wed, 6 Apr 2022 09:15:20 +0800 Subject: [PATCH] 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 --- .../0196.Delete Duplicate Emails/README.md | 31 +++++++++++-------- .../0196.Delete Duplicate Emails/README_EN.md | 31 +++++++++++-------- .../0196.Delete Duplicate Emails/Solution.sql | 21 +++++++------ .../README.md | 18 +++++++++++ .../README_EN.md | 18 +++++++++++ .../Solution.rs | 13 ++++++++ .../README.md | 26 ++++++++++++++++ .../README_EN.md | 26 ++++++++++++++++ .../Solution.rs | 21 +++++++++++++ .../README.md | 21 +++++++++++++ .../README_EN.md | 19 ++++++++++++ .../Solution.rs | 14 +++++++++ .../1873.Calculate Special Bonus/README.md | 23 ++++++++++++++ .../1873.Calculate Special Bonus/README_EN.md | 23 ++++++++++++++ .../1873.Calculate Special Bonus/Solution.SQL | 9 ++++++ 15 files changed, 279 insertions(+), 35 deletions(-) create mode 100644 solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.rs create mode 100644 solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.rs create mode 100644 solution/1800-1899/1822.Sign of the Product of an Array/Solution.rs create mode 100644 solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README.md b/solution/0100-0199/0196.Delete Duplicate Emails/README.md index 6df306fa2896a..5503114de140c 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README.md @@ -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; ``` diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md index 89a48b9200063..b24a2439ad256 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md @@ -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; ``` diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql b/solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql index 84ae283978803..ede7eeaffbafc 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql +++ b/solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql @@ -1,9 +1,12 @@ -delete from Person -where Id not in ( - select min(Id) - from ( - select * - from Person - ) as p - group by p.Email - ) \ No newline at end of file +DELETE +FROM + Person +WHERE + Id NOT IN ( + SELECT + MIN( Id ) + FROM + ( SELECT * FROM Person ) AS p + GROUP BY + p.Email + ); \ No newline at end of file diff --git a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md index 3a00bcbdbc487..f44360e44b653 100644 --- a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md +++ b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md @@ -91,6 +91,24 @@ var canMakeArithmeticProgression = function (arr) { }; ``` +### **Rust** + +```rust +impl Solution { + pub fn can_make_arithmetic_progression(mut arr: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md index ed29d84d93a80..3c9855c3d076a 100644 --- a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md +++ b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md @@ -81,6 +81,24 @@ var canMakeArithmeticProgression = function (arr) { }; ``` +### **Rust** + +```rust +impl Solution { + pub fn can_make_arithmetic_progression(mut arr: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.rs b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.rs new file mode 100644 index 0000000000000..c60f0abe5e830 --- /dev/null +++ b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn can_make_arithmetic_progression(mut arr: Vec) -> 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 + } +} diff --git a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md index c9374bae717e7..2a8b0e95e0b68 100644 --- a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md +++ b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md @@ -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 + } +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md index e406a610b76dd..6c7cdce60175c 100644 --- a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md +++ b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md @@ -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 + } +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.rs b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.rs new file mode 100644 index 0000000000000..84f15007d155c --- /dev/null +++ b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.rs @@ -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 + } +} diff --git a/solution/1800-1899/1822.Sign of the Product of an Array/README.md b/solution/1800-1899/1822.Sign of the Product of an Array/README.md index 44388e918f85f..7f812ad32e1ea 100644 --- a/solution/1800-1899/1822.Sign of the Product of an Array/README.md +++ b/solution/1800-1899/1822.Sign of the Product of an Array/README.md @@ -57,6 +57,8 @@ +不可模拟乘积过程,给定的范围有可能导致数值溢出,只关注数值的符号变化即可。 + ### **Python3** @@ -146,6 +148,25 @@ func arraySign(nums []int) int { } ``` +### **Rust** + +```rust +use std::cmp::Ordering; +impl Solution { + pub fn array_sign(nums: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md b/solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md index 7850d72304c77..b926c613c7202 100644 --- a/solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md +++ b/solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md @@ -136,6 +136,25 @@ func arraySign(nums []int) int { } ``` +### **Rust** + +```rust +use std::cmp::Ordering; +impl Solution { + pub fn array_sign(nums: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1822.Sign of the Product of an Array/Solution.rs b/solution/1800-1899/1822.Sign of the Product of an Array/Solution.rs new file mode 100644 index 0000000000000..17e03a6ff2a3a --- /dev/null +++ b/solution/1800-1899/1822.Sign of the Product of an Array/Solution.rs @@ -0,0 +1,14 @@ +use std::cmp::Ordering; +impl Solution { + pub fn array_sign(nums: Vec) -> 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 + } +} diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README.md b/solution/1800-1899/1873.Calculate Special Bonus/README.md index bba001f533c9f..31de1f066b0ae 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README.md @@ -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; ``` diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md index e011680e655da..8fb624d80aaba 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md @@ -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; ``` diff --git a/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL b/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL new file mode 100644 index 0000000000000..b38b97327da59 --- /dev/null +++ b/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL @@ -0,0 +1,9 @@ +SELECT + employee_id, + CASE + WHEN employee_id % 2 = 0 + OR LEFT(name, 1) = 'M' THEN 0 + ELSE salary + END AS bonus +FROM + employees; \ No newline at end of file