forked from aylei/leetcode-rust
-
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.
- Loading branch information
Showing
7 changed files
with
403 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* [10] Regular Expression Matching | ||
* | ||
* Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. | ||
* | ||
* | ||
* '.' Matches any single character. | ||
* '*' Matches zero or more of the preceding element. | ||
* | ||
* | ||
* The matching should cover the entire input string (not partial). | ||
* | ||
* Note: | ||
* | ||
* | ||
* s could be empty and contains only lowercase letters a-z. | ||
* p could be empty and contains only lowercase letters a-z, and characters like . or *. | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: | ||
* s = "aa" | ||
* p = "a" | ||
* Output: false | ||
* Explanation: "a" does not match the entire string "aa". | ||
* | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: | ||
* s = "aa" | ||
* p = "a*" | ||
* Output: true | ||
* Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". | ||
* | ||
* | ||
* Example 3: | ||
* | ||
* | ||
* Input: | ||
* s = "ab" | ||
* p = ".*" | ||
* Output: true | ||
* Explanation: ".*" means "zero or more (*) of any character (.)". | ||
* | ||
* | ||
* Example 4: | ||
* | ||
* | ||
* Input: | ||
* s = "aab" | ||
* p = "c*a*b" | ||
* Output: true | ||
* Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". | ||
* | ||
* | ||
* Example 5: | ||
* | ||
* | ||
* Input: | ||
* s = "mississippi" | ||
* p = "mis*is*p*." | ||
* Output: false | ||
* | ||
* | ||
*/ | ||
pub struct Solution {} | ||
|
||
// submission codes start here | ||
|
||
// TODO: NFA | ||
impl Solution { | ||
pub fn is_match(s: String, p: String) -> bool { | ||
false | ||
} | ||
} | ||
|
||
// submission codes end | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_10() { | ||
} | ||
} |
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,68 @@ | ||
/** | ||
* [11] Container With Most Water | ||
* | ||
* Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. | ||
* | ||
* Note: You may not slant the container and n is at least 2. | ||
* | ||
* | ||
* | ||
* <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" /> | ||
* | ||
* <small>The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. </small> | ||
* | ||
* | ||
* | ||
* Example: | ||
* | ||
* | ||
* Input: [1,8,6,2,5,4,8,3,7] | ||
* Output: 49 | ||
* | ||
*/ | ||
pub struct Solution {} | ||
|
||
// submission codes start here | ||
|
||
// Brute force: O(N^2) | ||
|
||
// Two Pointer: a[0] -> <- a[n-1] | ||
impl Solution { | ||
pub fn max_area(height: Vec<i32>) -> i32 { | ||
let (mut start, mut end) = (0_usize, (height.len() - 1)); | ||
let mut max: i32 = (end - start) as i32 * Solution::min(height[start], height[end]); | ||
let mut curr_area: i32 = 0; | ||
while end - start > 1 { | ||
// move the lower one | ||
if height[start] < height[end] { | ||
start += 1; | ||
if height[start] < height[start - 1] { continue } | ||
} else { | ||
end -= 1; | ||
if height[end] < height[end + 1] { continue } | ||
} | ||
curr_area = (end - start) as i32 * Solution::min(height[start], height[end]); | ||
if curr_area > max { max = curr_area } | ||
} | ||
max | ||
} | ||
|
||
#[inline(always)] | ||
fn min(i: i32, j: i32) -> i32 { | ||
if i > j { j } else { i } | ||
} | ||
} | ||
|
||
// submission codes end | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_11() { | ||
assert_eq!(Solution::max_area(vec![1, 8, 6, 2, 5, 4, 8, 3, 7]), 49); | ||
assert_eq!(Solution::max_area(vec![6, 9]), 6); | ||
assert_eq!(Solution::max_area(vec![1, 1, 2, 1, 1, 1]), 5); | ||
} | ||
} |
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,112 @@ | ||
/** | ||
* [12] Integer to Roman | ||
* | ||
* Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. | ||
* | ||
* | ||
* Symbol Value | ||
* I 1 | ||
* V 5 | ||
* X 10 | ||
* L 50 | ||
* C 100 | ||
* D 500 | ||
* M 1000 | ||
* | ||
* For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. | ||
* | ||
* Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: | ||
* | ||
* | ||
* I can be placed before V (5) and X (10) to make 4 and 9. | ||
* X can be placed before L (50) and C (100) to make 40 and 90. | ||
* C can be placed before D (500) and M (1000) to make 400 and 900. | ||
* | ||
* | ||
* Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: 3 | ||
* Output: "III" | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: 4 | ||
* Output: "IV" | ||
* | ||
* Example 3: | ||
* | ||
* | ||
* Input: 9 | ||
* Output: "IX" | ||
* | ||
* Example 4: | ||
* | ||
* | ||
* Input: 58 | ||
* Output: "LVIII" | ||
* Explanation: L = 50, V = 5, III = 3. | ||
* | ||
* | ||
* Example 5: | ||
* | ||
* | ||
* Input: 1994 | ||
* Output: "MCMXCIV" | ||
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. | ||
* | ||
*/ | ||
pub struct Solution {} | ||
|
||
// submission codes start here | ||
|
||
impl Solution { | ||
|
||
pub fn int_to_roman(num: i32) -> String { | ||
let table: Vec<(i32, &'static str)> = vec![ | ||
(1000, "M"), | ||
(900, "CM"), | ||
(500, "D"), | ||
(400, "CD"), | ||
(100, "C"), | ||
(90, "XC"), | ||
(50, "L"), | ||
(40, "XL"), | ||
(10, "X"), | ||
(9, "IX"), | ||
(5, "V"), | ||
(4, "IV"), | ||
(1, "I") | ||
]; | ||
|
||
let mut num = num; | ||
let mut sb = String::new(); | ||
for p in table.iter() { | ||
if num >= p.0 { | ||
for _ in 0..(num / p.0) { | ||
sb.push_str(p.1); | ||
} | ||
num = num % p.0 | ||
} | ||
} | ||
sb | ||
} | ||
} | ||
|
||
// submission codes end | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_12() { | ||
assert_eq!(Solution::int_to_roman(3), "III"); | ||
assert_eq!(Solution::int_to_roman(4), "IV"); | ||
assert_eq!(Solution::int_to_roman(9), "IX"); | ||
assert_eq!(Solution::int_to_roman(1994), "MCMXCIV"); | ||
} | ||
} |
Oops, something went wrong.