Skip to content

Commit 7942c19

Browse files
committed
07-28-2020
1 parent 0a061a3 commit 7942c19

5 files changed

+123
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct Solution;
2+
use std::iter::FromIterator;
3+
4+
impl Solution {
5+
pub fn longest_palindrome(s: String) -> String {
6+
if s == "" {
7+
return s;
8+
}
9+
let mut start: usize = 0;
10+
let mut end: usize = 0;
11+
let s: Vec<char> = s.chars().collect();
12+
13+
for i in 0..s.len() {
14+
let mut left: usize = i;
15+
let mut right: usize = i;
16+
while right + 1 < s.len() && s[right + 1] == s[left] {
17+
right += 1;
18+
}
19+
while left > 0 && right < s.len() - 1 && s[left - 1] == s[right + 1] {
20+
left -= 1;
21+
right += 1;
22+
}
23+
if right - left > end - start {
24+
start = left;
25+
end = right;
26+
}
27+
}
28+
String::from_iter(&s[start..=end])
29+
}
30+
}
31+
32+
#[test]
33+
fn test() {
34+
assert_eq!(
35+
Solution::longest_palindrome("babad".to_string()),
36+
"bab".to_string()
37+
);
38+
assert_eq!(
39+
Solution::longest_palindrome("cbbd".to_string()),
40+
"bb".to_string()
41+
);
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn max_area(height: Vec<i32>) -> i32 {
5+
let mut h_left: usize = 0;
6+
let mut h_right: usize = height.len() - 1;
7+
let mut max_area: i32 = 0;
8+
9+
while h_left < h_right {
10+
let h_area: i32 = height[h_left].min(height[h_right]) * (h_right - h_left) as i32;
11+
max_area = max_area.max(h_area);
12+
if height[h_left] < height[h_right] {
13+
h_left += 1;
14+
} else {
15+
h_right -= 1;
16+
}
17+
}
18+
max_area
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
assert_eq!(Solution::max_area(vec![1, 8, 6, 2, 5, 4, 8, 3, 7]), 49);
25+
}

src/_0198_house_robber.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn rob(nums: Vec<i32>) -> i32 {
5+
let mut h_previous_max: i32 = 0;
6+
let mut h_current_max: i32 = 0;
7+
8+
for v in nums {
9+
let h_temp: i32 = h_current_max;
10+
h_current_max = (h_previous_max + v).max(h_current_max);
11+
h_previous_max = h_temp;
12+
}
13+
h_current_max
14+
}
15+
}
16+
17+
#[test]
18+
fn test() {
19+
assert_eq!(Solution::rob(vec![1, 2, 3, 1]), 4);
20+
assert_eq!(Solution::rob(vec![2, 7, 9, 3, 1]), 12);
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct Solution;
2+
impl Solution {
3+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
4+
let length: usize = nums.len();
5+
let mut answer: Vec<i32> = vec![1; length];
6+
7+
let mut h_product: i32 = 1;
8+
for i in 0..length {
9+
answer[i] = h_product;
10+
h_product *= nums[i]
11+
}
12+
h_product = 1;
13+
for i in (0..length).rev() {
14+
answer[i] *= h_product;
15+
h_product *= nums[i];
16+
}
17+
answer
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
assert_eq!(
24+
Solution::product_except_self(vec![1, 2, 3, 4]),
25+
vec![24, 12, 8, 6]
26+
);
27+
}

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ mod _0001_two_sum;
1414
//
1515
mod _0002_add_two_numbers;
1616
//
17+
mod _0005_longest_palindromic_substring;
18+
//
1719
mod _0007_reverse_integer;
1820
//
1921
mod _0009_palindrome_number;
2022
//
23+
mod _0011_container_with_most_water;
24+
//
2125
mod _0014_longest_common_prefix;
2226
//
2327
mod _0015_3sum;
@@ -64,6 +68,8 @@ mod _0169_majority_element;
6468
//
6569
mod _0189_rotate_array;
6670
//
71+
mod _0198_house_robber;
72+
//
6773
mod _0200_number_of_islands;
6874
//
6975
mod _0202_happy_number;
@@ -78,6 +84,8 @@ mod _0210_course_schedule_ii;
7884
//
7985
mod _0219_contains_duplicate_ii;
8086
//
87+
mod _0238_product_of_array_except_self;
88+
//
8189
mod _0242_valid_anagram;
8290
//
8391
mod _0243_shortest_word_distance;

0 commit comments

Comments
 (0)