Browse Source

feat: add simplify_path

Zhang Li 1 year ago
parent
commit
a4129ee48f

+ 36 - 0
src/solutions/dp/climbing_stairs.rs

@@ -0,0 +1,36 @@
+struct Solution;
+
+impl Solution {
+    pub fn climb_stairs(n: i32) -> i32 {
+
+
+        let mut a = 1;
+        let mut b = 2;
+        match n {
+            1 => return 1,
+            2 => return 2,
+            _ => {
+                for _ in 3..=n {
+                    let tmp = a + b;
+                    a = b;
+                    b = tmp;
+                }
+            }
+        }
+        b
+
+    }
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_climb_stairs() {
+        let n = 4;
+        let res = Solution::climb_stairs(n);
+        assert_eq!(res, 5);
+    }
+}

+ 1 - 0
src/solutions/dp/mod.rs

@@ -7,3 +7,4 @@ mod trapping_train_water;
 mod unique_paths;
 mod unique_paths_2;
 mod edit_distance;
+mod climbing_stairs;

+ 2 - 1
src/solutions/mod.rs

@@ -4,6 +4,7 @@ mod add_two_numbers;
 mod array;
 mod binary_search;
 mod bt;
+mod stack;
 mod container_with_most_water;
 mod dp;
 mod four_sum;
@@ -30,7 +31,7 @@ mod string_to_integer;
 mod swap_nodes_in_pairs;
 mod three_sum;
 mod three_sum_closest;
-pub mod two_sum;
+mod two_sum;
 mod valid_parenthese;
 mod zigzag_conversion;
 

+ 1 - 0
src/solutions/stack/mod.rs

@@ -0,0 +1 @@
+mod simplify_path;

+ 112 - 0
src/solutions/stack/simplify_path.rs

@@ -0,0 +1,112 @@
+struct Solution;
+
+
+impl Solution {
+    pub fn simplify_path(path: String) -> String {
+
+        let mut v: Vec<char> = path.char_indices().map(|(_, c)| c).collect();
+
+        let l = v.len();
+        if v[l-1] != '/' {
+            v.push('/');
+        }
+        let l = v.len();
+        let mut i = 0;
+        let mut stack = vec![];
+        let mut top = 0;
+        let mut flag = 0;
+        while i < l {
+            if i == 0 {
+                stack.push(v[i]);
+                top = i;
+
+            }  else if v[i] == '.' && stack[top] == '/' {
+                flag += 1;
+                stack.push(v[i]);
+            } else if flag == 1 && v[i] == '/' {
+                stack.pop();
+                top = stack.len() - 1;
+                flag = 0;
+            } else if flag == 2 && v[i] == '/' {
+                stack.pop();
+                stack.pop();
+                while top > 0 {
+                    stack.pop();
+                    top -=1;
+                    if stack[top] == '/'  {
+                        break;
+                    }
+
+                }
+                flag = 0;
+            } else if flag == 0 && v[i] == '/' && stack[top] == '/' {
+
+            } else {
+                stack.push(v[i]);
+                top = stack.len() - 1;
+                flag = 0;
+            }
+            i += 1;
+
+            // println!("stack : {:?}, flag: {}, top: {}", stack, flag, top);
+        }
+        if stack.len() > 1 && stack[stack.len() - 1] == '/' {
+            stack.pop();
+        }
+
+        let res = stack.into_iter().collect::<String>();
+        res
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_simplify_path1() {
+        let s = String::from("/Home/Users/");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/Home/Users", res);
+    }
+
+    #[test]
+    fn test_simplify_path2() {
+        let s = String::from("/Home//.././Users/");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/Users", res);
+    }
+
+
+
+
+
+    #[test]
+    fn test_simplify_path3() {
+        let s = String::from("/../");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/", res);
+    }
+    #[test]
+    fn test_simplify_path4() {
+        let s = String::from("/Home//./Users/");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/Home/Users", res);
+    }
+
+    #[test]
+    fn test_simplify_path5() {
+        let s = String::from("/Home//.../Users/");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/Home/.../Users", res);
+    }
+
+
+    #[test]
+    fn test_simplify_path6() {
+        let s = String::from("/a//b////c/d//././/..");
+        let res = Solution::simplify_path(s);
+        assert_eq!("/a/b/c", res);
+    }
+}