Browse Source

feat: compare_version_number

Zhang Li 11 months ago
parent
commit
c2b70f1840

+ 2 - 1
src/solutions/binary_search/mod.rs

@@ -9,4 +9,5 @@ mod binary_tree_preorder_traversal;
 mod binary_tree_postorder_traversal;
 mod find_minimum_in_rotated_array;
 mod find_minimum_in_rotated_array2;
-mod find_peak_element;
+mod find_peak_element;
+mod two_sum;

+ 68 - 0
src/solutions/binary_search/two_sum.rs

@@ -0,0 +1,68 @@
+struct Solution;
+impl Solution {
+    pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
+        let n = numbers.len();
+        for i in 0..n-1 {
+            let elem = target - numbers[i];
+
+            if let Some(e) = Self::binary_search(&numbers, elem, i+1, n-1) {
+                return vec![i as i32+1, e];
+            }
+        }
+
+        vec![]
+
+    }
+
+    fn binary_search(numbers: &Vec<i32>, elem: i32, left: usize, right: usize) -> Option<i32> {
+        if numbers[left] > elem {
+            return None;
+        }
+        let mut left = left;
+        let mut right = right;
+        while left <= right {
+            let mid = (left + right) / 2;
+            if numbers[mid] == elem {
+                return Some(1 + mid as i32);
+            }
+
+            if numbers[mid] > elem {
+                if mid == 0 {
+                    return None;
+                }
+                right = mid - 1;
+                continue;
+            }
+            if numbers[mid] < elem {
+                left = mid + 1;
+                continue;
+            }
+
+        }
+
+        None
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_two_sum() {
+        let numbers = vec![2,7,11,15];
+        let target = 9;
+        let res = Solution::two_sum(numbers, target);
+        assert_eq!(res, vec![1,2]);
+    }
+
+    #[test]
+    fn test_two_sum2() {
+        let numbers = vec![5,25,75];
+        let target = 100;
+        let res = Solution::two_sum(numbers, target);
+        assert_eq!(res, vec![2,3]);
+    }
+
+}

+ 31 - 0
src/solutions/str/compare_version_numbers.rs

@@ -0,0 +1,31 @@
+struct Solution;
+
+impl Solution {
+    pub fn compare_version(version1: String, version2: String) -> i32 {
+        let mut version1 = version1.split(".").into_iter().map(|x| x.parse().unwrap());
+        let mut version2= version2.split(".").into_iter().map(|x| x.parse().unwrap());
+        loop {
+            match (version1.next(), version2.next()) {
+                (Some(version1), version2) if version1 > version2.unwrap_or(0) => return 1,
+                (version1, Some(version2)) if version1.unwrap_or(0) < version2 => return -1,
+                (None, None) => return 0,
+                (_, _) => continue,
+            }
+        }
+
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_compare_version() {
+        let version1 = String::from("1.01");
+        let version2 = String::from("1.001");
+        let res = Solution::compare_version(version1, version2);
+        assert_eq!(res, 0);
+    }
+}

+ 2 - 1
src/solutions/str/mod.rs

@@ -3,4 +3,5 @@ mod strstr;
 mod minimum_window_substring;
 mod string_to_integer;
 mod valid_palindrome;
-mod reverse_words_in_a_string;
+mod reverse_words_in_a_string;
+mod compare_version_numbers;