forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
15 changed files
with
279 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 12 additions & 9 deletions
21
solution/0100-0199/0196.Delete Duplicate Emails/Solution.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
solution/1800-1899/1822.Sign of the Product of an Array/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.