Skip to content

Commit 51dce9d

Browse files
committedJan 16, 2019
1 parent 947aa64 commit 51dce9d

7 files changed

+403
-4
lines changed
 

‎src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ mod n0006_zigzag_conversion;
77
mod n0007_reverse_integer;
88
mod n0008_string_to_integer_atoi;
99
mod n0009_palindrome_number;
10+
mod n0010_regular_expression_matching;
11+
mod n0011_container_with_most_water;
12+
mod n0012_integer_to_roman;
13+
mod n0013_roman_to_integer;

‎src/main.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn main() {
5858
}
5959

6060
fn build_desc(content: &str) -> String {
61+
// TODO: fix this shit
6162
content
6263
.replace("<strong>", "")
6364
.replace("</strong>", "")
@@ -67,13 +68,24 @@ fn build_desc(content: &str) -> String {
6768
.replace("<p>", "")
6869
.replace("<b>", "")
6970
.replace("</b>", "")
70-
.replace("</pre>", "")
7171
.replace("<pre>", "")
72+
.replace("</pre>", "")
73+
.replace("<ul>", "")
74+
.replace("</ul>", "")
75+
.replace("<li>", "")
76+
.replace("</li>", "")
77+
.replace("<code>", "")
78+
.replace("</code>", "")
79+
.replace("<i>", "")
80+
.replace("</i>", "")
81+
.replace("<sub>", "")
82+
.replace("</sub>", "")
83+
.replace("</sup>", "")
84+
.replace("<sup>", "^")
7285
.replace("&nbsp;", " ")
7386
.replace("&quot;", "\"")
87+
.replace("&minus;", "-")
88+
.replace("&#39;", "'")
7489
.replace("\n\n", "\n")
7590
.replace("\n", "\n * ")
76-
.replace("&minus;", "-")
77-
.replace("</sup>", "")
78-
.replace("<sup>", "^")
7991
}

‎src/n0004_median_of_two_sorted_arrays.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Solution {}
3030

3131
// submission codes start here
3232

33+
// TODO: nth slice
3334
impl Solution {
3435
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
3536
1.0
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* [10] Regular Expression Matching
3+
*
4+
* Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
5+
*
6+
*
7+
* '.' Matches any single character.
8+
* '*' Matches zero or more of the preceding element.
9+
*
10+
*
11+
* The matching should cover the entire input string (not partial).
12+
*
13+
* Note:
14+
*
15+
*
16+
* s could be empty and contains only lowercase letters a-z.
17+
* p could be empty and contains only lowercase letters a-z, and characters like . or *.
18+
*
19+
*
20+
* Example 1:
21+
*
22+
*
23+
* Input:
24+
* s = "aa"
25+
* p = "a"
26+
* Output: false
27+
* Explanation: "a" does not match the entire string "aa".
28+
*
29+
*
30+
* Example 2:
31+
*
32+
*
33+
* Input:
34+
* s = "aa"
35+
* p = "a*"
36+
* Output: true
37+
* Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
38+
*
39+
*
40+
* Example 3:
41+
*
42+
*
43+
* Input:
44+
* s = "ab"
45+
* p = ".*"
46+
* Output: true
47+
* Explanation: ".*" means "zero or more (*) of any character (.)".
48+
*
49+
*
50+
* Example 4:
51+
*
52+
*
53+
* Input:
54+
* s = "aab"
55+
* p = "c*a*b"
56+
* Output: true
57+
* Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
58+
*
59+
*
60+
* Example 5:
61+
*
62+
*
63+
* Input:
64+
* s = "mississippi"
65+
* p = "mis*is*p*."
66+
* Output: false
67+
*
68+
*
69+
*/
70+
pub struct Solution {}
71+
72+
// submission codes start here
73+
74+
// TODO: NFA
75+
impl Solution {
76+
pub fn is_match(s: String, p: String) -> bool {
77+
false
78+
}
79+
}
80+
81+
// submission codes end
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_10() {
89+
}
90+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* [11] Container With Most Water
3+
*
4+
* 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.
5+
*
6+
* Note: You may not slant the container and n is at least 2.
7+
*
8+
*
9+
*
10+
* <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" />
11+
*
12+
* <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>
13+
*
14+
*
15+
*
16+
* Example:
17+
*
18+
*
19+
* Input: [1,8,6,2,5,4,8,3,7]
20+
* Output: 49
21+
*
22+
*/
23+
pub struct Solution {}
24+
25+
// submission codes start here
26+
27+
// Brute force: O(N^2)
28+
29+
// Two Pointer: a[0] -> <- a[n-1]
30+
impl Solution {
31+
pub fn max_area(height: Vec<i32>) -> i32 {
32+
let (mut start, mut end) = (0_usize, (height.len() - 1));
33+
let mut max: i32 = (end - start) as i32 * Solution::min(height[start], height[end]);
34+
let mut curr_area: i32 = 0;
35+
while end - start > 1 {
36+
// move the lower one
37+
if height[start] < height[end] {
38+
start += 1;
39+
if height[start] < height[start - 1] { continue }
40+
} else {
41+
end -= 1;
42+
if height[end] < height[end + 1] { continue }
43+
}
44+
curr_area = (end - start) as i32 * Solution::min(height[start], height[end]);
45+
if curr_area > max { max = curr_area }
46+
}
47+
max
48+
}
49+
50+
#[inline(always)]
51+
fn min(i: i32, j: i32) -> i32 {
52+
if i > j { j } else { i }
53+
}
54+
}
55+
56+
// submission codes end
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn test_11() {
64+
assert_eq!(Solution::max_area(vec![1, 8, 6, 2, 5, 4, 8, 3, 7]), 49);
65+
assert_eq!(Solution::max_area(vec![6, 9]), 6);
66+
assert_eq!(Solution::max_area(vec![1, 1, 2, 1, 1, 1]), 5);
67+
}
68+
}

‎src/n0012_integer_to_roman.rs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* [12] Integer to Roman
3+
*
4+
* Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
5+
*
6+
*
7+
* Symbol Value
8+
* I 1
9+
* V 5
10+
* X 10
11+
* L 50
12+
* C 100
13+
* D 500
14+
* M 1000
15+
*
16+
* 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.
17+
*
18+
* 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:
19+
*
20+
*
21+
* I can be placed before V (5) and X (10) to make 4 and 9.
22+
* X can be placed before L (50) and C (100) to make 40 and 90.
23+
* C can be placed before D (500) and M (1000) to make 400 and 900.
24+
*
25+
*
26+
* Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
27+
*
28+
* Example 1:
29+
*
30+
*
31+
* Input: 3
32+
* Output: "III"
33+
*
34+
* Example 2:
35+
*
36+
*
37+
* Input: 4
38+
* Output: "IV"
39+
*
40+
* Example 3:
41+
*
42+
*
43+
* Input: 9
44+
* Output: "IX"
45+
*
46+
* Example 4:
47+
*
48+
*
49+
* Input: 58
50+
* Output: "LVIII"
51+
* Explanation: L = 50, V = 5, III = 3.
52+
*
53+
*
54+
* Example 5:
55+
*
56+
*
57+
* Input: 1994
58+
* Output: "MCMXCIV"
59+
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
60+
*
61+
*/
62+
pub struct Solution {}
63+
64+
// submission codes start here
65+
66+
impl Solution {
67+
68+
pub fn int_to_roman(num: i32) -> String {
69+
let table: Vec<(i32, &'static str)> = vec![
70+
(1000, "M"),
71+
(900, "CM"),
72+
(500, "D"),
73+
(400, "CD"),
74+
(100, "C"),
75+
(90, "XC"),
76+
(50, "L"),
77+
(40, "XL"),
78+
(10, "X"),
79+
(9, "IX"),
80+
(5, "V"),
81+
(4, "IV"),
82+
(1, "I")
83+
];
84+
85+
let mut num = num;
86+
let mut sb = String::new();
87+
for p in table.iter() {
88+
if num >= p.0 {
89+
for _ in 0..(num / p.0) {
90+
sb.push_str(p.1);
91+
}
92+
num = num % p.0
93+
}
94+
}
95+
sb
96+
}
97+
}
98+
99+
// submission codes end
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use super::*;
104+
105+
#[test]
106+
fn test_12() {
107+
assert_eq!(Solution::int_to_roman(3), "III");
108+
assert_eq!(Solution::int_to_roman(4), "IV");
109+
assert_eq!(Solution::int_to_roman(9), "IX");
110+
assert_eq!(Solution::int_to_roman(1994), "MCMXCIV");
111+
}
112+
}

0 commit comments

Comments
 (0)
Failed to load comments.