Skip to content

Commit

Permalink
add refer leetcode problems
Browse files Browse the repository at this point in the history
  • Loading branch information
phinnl committed May 7, 2024
1 parent b33ccf3 commit 2b952e7
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/add_two_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub struct ListNode {
pub next: Option<Box<ListNode>>,
}

// refer to https://leetcode.com/problems/add-two-numbers/

pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
Expand Down
28 changes: 28 additions & 0 deletions src/best_time_to_buy_and_sell_stock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// refer to https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

pub fn max_profit(prices: Vec<i32>) -> i32 {
prices.iter()
.skip(1)
.fold((0, prices[0]), |(diff, min), &n| match n <= min {
true => (diff, n),
false => (diff.max(n - min), min),
})
.0
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn example_1() {
let prices = vec![7, 1, 5, 3, 6, 4];
assert_eq!(max_profit(prices), 5);
}

#[test]
fn example_2() {
let prices = vec![7, 6, 4, 3, 1];
assert_eq!(max_profit(prices), 0);
}
}
2 changes: 2 additions & 0 deletions src/container_with_most_water.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/container-with-most-water/

pub fn max_area(height: Vec<i32>) -> i32 {
let mut max = 0;
let mut left = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/integer_to_roman.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/integer-to-roman/

pub fn int_to_roman(num: i32) -> String {
const RADIX: u32 = 10;
let roman = ["I", "V", "X", "L", "C", "D", "M"];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub fn find_longest_substring(s: String) -> i32 {
// refer to https://leetcode.com/problems/longest-substring-without-repeating-characters/

pub fn length_of_longest_substring(s: String) -> i32 {
let mut length = 0;
let mut st = String::new();
for item in s.chars() {
Expand Down Expand Up @@ -26,7 +28,7 @@ mod tests {
#[test]
fn example_1() {
let input = "abcabcbb".to_owned();
let output = find_longest_substring(input.clone());
let output = length_of_longest_substring(input.clone());
let expect = 3;
assert_eq!(
output, expect,
Expand All @@ -36,7 +38,7 @@ mod tests {
#[test]
fn example_2() {
let input = "bbbbb".to_owned();
let output = find_longest_substring(input.clone());
let output = length_of_longest_substring(input.clone());
let expect = 1;
assert_eq!(
output, expect,
Expand All @@ -46,7 +48,7 @@ mod tests {
#[test]
fn example_3() {
let input = "pwwkew".to_owned();
let output = find_longest_substring(input.clone());
let output = length_of_longest_substring(input.clone());
let expect = 3;
assert_eq!(
output, expect,
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub mod add_two_sum;
pub mod two_sum;
pub mod add_two_numbers;
pub mod find_longest_substring;
pub mod length_of_longest_substring;
pub mod median_of_two_arr;
pub mod longest_palindrome;
pub mod convert_zigzag_string;
pub mod zigzag_conversion;
pub mod reverse_int;
pub mod my_atoi;
pub mod is_palindromic;
pub mod palindrome_number;
pub mod regex_matching;
pub mod container_with_most_water;
pub mod integer_to_roman;
Expand All @@ -15,4 +15,6 @@ pub mod longest_common_prefix;
pub mod three_sum;
pub mod max_sub_array;
pub mod three_sum_closest;
pub mod repeated_character;
pub mod repeated_character;
pub mod best_time_to_buy_and_sell_stock;
pub mod maximum_different_between_increasing_elements;
2 changes: 2 additions & 0 deletions src/longest_common_prefix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/longest-common-prefix/

pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut result = String::new();
let first_str_bytes = strs[0].as_bytes();
Expand Down
2 changes: 2 additions & 0 deletions src/longest_palindrome.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

// refer to https://leetcode.com/problems/longest-palindrome/

pub fn longest_palindrome(s: String) -> String {
let equal = |option: Option<&char>, item: char| -> bool {
match option {
Expand Down
2 changes: 2 additions & 0 deletions src/max_sub_array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::cmp::max;

// refer to https://leetcode.com/problems/maximum-subarray/

pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let mut result = i32::MIN;
let mut sum = 0;
Expand Down
48 changes: 48 additions & 0 deletions src/maximum_different_between_increasing_elements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// refer to https://leetcode.com/problems/maximum-difference-between-increasing-elements/

pub fn maximum_difference(nums: Vec<i32>) -> i32 {
let length = nums.len();
if length < 1 {
return -1;
}
let max_option = nums
.iter()
.enumerate()
.map(|(index, item)| {
if index == length {
return -1;
}
let next_index = index + 1;
let child_max = nums[next_index..]
.to_vec()
.iter()
.map(|next_item| {
if item >= next_item {
-1
} else {
next_item - item
}
})
.max();
match child_max {
Some(num) => num,
_ => -1,
}
})
.max();
match max_option {
Some(max) => max,
_ => -1,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn example_1() {
let nums = vec![7, 1, 5, 3, 6, 4];
assert_eq!(maximum_difference(nums), 5);
}
}
8 changes: 2 additions & 6 deletions src/median_of_two_arr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/median-of-two-sorted-arrays/

pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
let (mut nums1, mut nums2) = (nums1, nums2);
nums1.append(&mut nums2);
Expand All @@ -16,12 +18,6 @@ mod tests {

#[test]
fn example_1() {
// Input: nums1 = [1,3], nums2 = [2]
// Output: 2.00000
// Explanation: merged array = [1,2,3] and median is 2.
// Example 2:

// Input: nums1 = [1,2], nums2 = [3,4]
let input_1 = vec![1, 3];
let input_2 = vec![2];
let expect: f64 = 2.0;
Expand Down
2 changes: 2 additions & 0 deletions src/my_atoi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/string-to-integer-atoi/

pub fn my_atoi(s: String) -> i32 {
let bytes = s.as_bytes();
let mut temp = String::new();
Expand Down
9 changes: 7 additions & 2 deletions src/is_palindromic.rs → src/palindrome_number.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// refer to https://leetcode.com/problems/palindrome-number/

pub fn is_palindrome(x: i32) -> bool {
// 1. not fast -> x.to_string() == x.to_string().chars().rev().collect::<String>()
// 2. fast
let st = x.to_string();
let x_chars = st.as_bytes().iter().map(|&item| item as char).collect::<Vec<char>>();
for i in 0..x_chars.len() / 2 {
Expand All @@ -13,6 +13,10 @@ pub fn is_palindrome(x: i32) -> bool {
true
}

pub fn is_palindrome_2(x: i32) -> bool {
x.to_string() == x.to_string().chars().rev().collect::<String>()
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -21,5 +25,6 @@ mod tests {
fn example_1() {
let x = 121;
assert!(is_palindrome(x));
assert!(is_palindrome_2(x));
}
}
2 changes: 2 additions & 0 deletions src/regex_matching.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/regular-expression-matching/

pub fn is_match(_s: String, _p: String) -> bool {
true
}
Expand Down
2 changes: 2 additions & 0 deletions src/reverse_int.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/reverse-integer/

pub fn reverse(num: i32) -> i32 {
let is_positive = num > 0;
match num
Expand Down
2 changes: 2 additions & 0 deletions src/roman_to_integer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

// refer to https://leetcode.com/problems/roman-to-integer/

pub fn roman_to_int(s: String) -> i32 {
let mut result: i32 = 0;
let roman_map: HashMap<char, u32> = HashMap::from([
Expand Down
2 changes: 2 additions & 0 deletions src/three_sum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/3sum/

pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
nums.sort();
Expand Down
2 changes: 2 additions & 0 deletions src/three_sum_closest.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/3sum-closest/

pub fn three_sum_closest(mut nums: Vec<i32>, target: i32) -> i32 {
let mut result: Option<i32> = None;
nums.sort();
Expand Down
2 changes: 2 additions & 0 deletions src/add_two_sum.rs → src/two_sum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

// refer to https://leetcode.com/problems/two-sum/

pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map: HashMap<i32, i32> = HashMap::new();
for (index, &item) in nums.iter().enumerate() {
Expand Down
2 changes: 2 additions & 0 deletions src/convert_zigzag_string.rs → src/zigzag_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// refer to https://leetcode.com/problems/zigzag-conversion/

pub fn convert(s: String, num_rows: i32) -> String {
let len = s.len();
if len as i32 <= num_rows || num_rows == 1 {
Expand Down

0 comments on commit 2b952e7

Please sign in to comment.