Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aylei committed Jan 16, 2019
1 parent 947aa64 commit 51dce9d
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ mod n0006_zigzag_conversion;
mod n0007_reverse_integer;
mod n0008_string_to_integer_atoi;
mod n0009_palindrome_number;
mod n0010_regular_expression_matching;
mod n0011_container_with_most_water;
mod n0012_integer_to_roman;
mod n0013_roman_to_integer;
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn main() {
}

fn build_desc(content: &str) -> String {
// TODO: fix this shit
content
.replace("<strong>", "")
.replace("</strong>", "")
Expand All @@ -67,13 +68,24 @@ fn build_desc(content: &str) -> String {
.replace("<p>", "")
.replace("<b>", "")
.replace("</b>", "")
.replace("</pre>", "")
.replace("<pre>", "")
.replace("</pre>", "")
.replace("<ul>", "")
.replace("</ul>", "")
.replace("<li>", "")
.replace("</li>", "")
.replace("<code>", "")
.replace("</code>", "")
.replace("<i>", "")
.replace("</i>", "")
.replace("<sub>", "")
.replace("</sub>", "")
.replace("</sup>", "")
.replace("<sup>", "^")
.replace("&nbsp;", " ")
.replace("&quot;", "\"")
.replace("&minus;", "-")
.replace("&#39;", "'")
.replace("\n\n", "\n")
.replace("\n", "\n * ")
.replace("&minus;", "-")
.replace("</sup>", "")
.replace("<sup>", "^")
}
1 change: 1 addition & 0 deletions src/n0004_median_of_two_sorted_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Solution {}

// submission codes start here

// TODO: nth slice
impl Solution {
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
1.0
Expand Down
90 changes: 90 additions & 0 deletions src/n0010_regular_expression_matching.rs
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() {
}
}
68 changes: 68 additions & 0 deletions src/n0011_container_with_most_water.rs
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);
}
}
112 changes: 112 additions & 0 deletions src/n0012_integer_to_roman.rs
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");
}
}
Loading

0 comments on commit 51dce9d

Please sign in to comment.