Skip to content

Commit e04b542

Browse files
committed
add 336;583;647;680;696;763;807
1 parent 40a9e02 commit e04b542

7 files changed

+254
-0
lines changed

336.palindrome-pairs.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=336 lang=rust
3+
*
4+
* [336] Palindrome Pairs
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn palindrome_pairs(words: Vec<String>) -> Vec<Vec<i32>> {
10+
11+
}
12+
}
13+
// @lc code=end
14+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=583 lang=rust
3+
*
4+
* [583] Delete Operation for Two Strings
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
10+
impl Solution {
11+
pub fn min_distance(word1: String, word2: String) -> i32 {
12+
let chars1 = word1.chars().collect::<Vec<char>>();
13+
let chars2 = word2.chars().collect::<Vec<char>>();
14+
15+
let l1 = chars1.len();
16+
let l2 = chars2.len();
17+
18+
let mut dp = vec![vec![0; l2+1]; l1+1];
19+
20+
for i in 0..l1+1 {
21+
for j in 0..l2+1 {
22+
if i == 0 || j == 0 {
23+
dp[i][j] = 0;
24+
} else if chars1[i-1] == chars2[j-1] {
25+
dp[i][j] = 1 + dp[i-1][j-1];
26+
} else {
27+
dp[i][j] = cmp::max(dp[i-1][j], dp[i][j-1]);
28+
}
29+
}
30+
}
31+
32+
// println!("{:?}", dp);
33+
(l1 + l2) as i32 - 2 * dp[l1][l2]
34+
}
35+
}
36+
// @lc code=end
37+

647.palindromic-substrings.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=647 lang=rust
3+
*
4+
* [647] Palindromic Substrings
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn count_substrings(s: String) -> i32 {
10+
let chars = s.chars().collect::<Vec<char>>();
11+
let N = chars.len();
12+
13+
let mut ans = 0;
14+
let mut dp = vec![vec![false; N]; N];
15+
16+
for j in 0..N {
17+
for i in (0..j+1).rev() {
18+
if chars[i] == chars[j] && (i as i32 + 1 >= j as i32 - 1 || dp[i+1][j-1]) {
19+
ans += 1;
20+
dp[i][j] = true;
21+
}
22+
}
23+
}
24+
25+
ans
26+
}
27+
}
28+
// @lc code=end
29+

680.valid-palindrome-ii.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=680 lang=rust
3+
*
4+
* [680] Valid Palindrome II
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn valid_palindrome(s: String) -> bool {
10+
let chars = s.chars().collect::<Vec<char>>();
11+
let N = chars.len();
12+
13+
if N <= 1 { return true; }
14+
let mut i = 0;
15+
16+
while i < N / 2 {
17+
if chars[i] != chars[N - i - 1] {
18+
return Self::is_palindrome(&chars[i..N-i-1]) || Self::is_palindrome(&chars[i+1..N-i]);
19+
}
20+
i += 1;
21+
}
22+
23+
return true;
24+
}
25+
26+
pub fn is_palindrome(chars: &[char]) -> bool {
27+
// let chars = s.chars().collect::<Vec<char>>();
28+
let N = chars.len();
29+
30+
if N <= 1 { return true; }
31+
32+
let mut i = 0;
33+
34+
while i < N / 2 {
35+
if chars[i] != chars[N - i - 1] {
36+
return false;
37+
}
38+
i += 1;
39+
}
40+
41+
return true;
42+
}
43+
}
44+
// @lc code=end
45+

696.count-binary-substrings.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=696 lang=rust
3+
*
4+
* [696] Count Binary Substrings
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn count_binary_substrings(s: String) -> i32 {
10+
let chars = s.chars().collect::<Vec<char>>();
11+
let N = chars.len();
12+
13+
if N <= 1 { return 0; }
14+
let mut ans = 0;
15+
16+
for i in 0..N-1 {
17+
if (chars[i] == '0' && chars[i+1] == '1') ||
18+
(chars[i] == '1' && chars[i+1] == '0') {
19+
let mut k = 1;
20+
while i + k < N && i as i32 - k as i32 + 1 >= 0 {
21+
if chars[i+k] == chars[i+1] && chars[i-k+1] == chars[i] {
22+
k += 1;
23+
ans += 1;
24+
} else {
25+
break;
26+
}
27+
}
28+
}
29+
}
30+
31+
ans
32+
}
33+
}
34+
// @lc code=end
35+

763.partition-labels.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode id=763 lang=rust
3+
*
4+
* [763] Partition Labels
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
10+
impl Solution {
11+
pub fn partition_labels(s: String) -> Vec<i32> {
12+
let chars = s.chars().collect::<Vec<char>>();
13+
14+
let N = chars.len();
15+
16+
let mut ranges = vec![vec![-1, -1]; 26];
17+
18+
for i in 0..N {
19+
let ord = chars[i] as usize - 'a' as usize;
20+
if ranges[ord][0] == -1 {
21+
ranges[ord][0] = i as i32;
22+
ranges[ord][1] = i as i32;
23+
} else {
24+
ranges[ord][1] = i as i32;
25+
}
26+
}
27+
28+
ranges.sort_by_key(|k| k[0]);
29+
30+
let mut curr_end = -1;
31+
let mut ans = vec![];
32+
33+
for v in ranges {
34+
if v[0] >= 0 {
35+
if curr_end < v[0] {
36+
ans.push(v[0]);
37+
}
38+
curr_end = cmp::max(curr_end, v[1]);
39+
}
40+
}
41+
42+
if ans[ans.len() - 1] < N as i32 {
43+
ans.push(N as i32);
44+
}
45+
46+
let mut ans1 = vec![];
47+
48+
for i in 1..ans.len() {
49+
ans1.push(ans[i] - ans[i-1]);
50+
}
51+
52+
ans1
53+
}
54+
}
55+
// @lc code=end
56+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=807 lang=rust
3+
*
4+
* [807] Max Increase to Keep City Skyline
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
10+
impl Solution {
11+
pub fn max_increase_keeping_skyline(grid: Vec<Vec<i32>>) -> i32 {
12+
let R = grid.len();
13+
if R == 0 { return 0; }
14+
let C = grid[0].len();
15+
16+
let mut top_bottom_view = vec![0; C];
17+
let mut left_right_view = vec![0; R];
18+
19+
for i in 0..R {
20+
for j in 0..C {
21+
top_bottom_view[j] = cmp::max(top_bottom_view[j], grid[i][j]);
22+
left_right_view[i] = cmp::max(left_right_view[i], grid[i][j]);
23+
}
24+
}
25+
26+
let mut ans = 0;
27+
28+
for i in 0..R {
29+
for j in 0..C {
30+
ans += cmp::min(top_bottom_view[j], left_right_view[i]) - grid[i][j];
31+
}
32+
}
33+
34+
ans
35+
}
36+
}
37+
// @lc code=end
38+

0 commit comments

Comments
 (0)