|
| 1 | +/** |
| 2 | + * [165] Compare Version Numbers |
| 3 | + * |
| 4 | + * Compare two version numbers version1 and version2.<br /> |
| 5 | + * If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. |
| 6 | + * |
| 7 | + * You may assume that the version strings are non-empty and contain only digits and the . character. |
| 8 | + * The . character does not represent a decimal point and is used to separate number sequences. |
| 9 | + * For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision. |
| 10 | + * You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0. |
| 11 | + * |
| 12 | + * |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * |
| 16 | + * Input: version1 = "0.1", version2 = "1.1" |
| 17 | + * Output: -1 |
| 18 | + * |
| 19 | + * Example 2: |
| 20 | + * |
| 21 | + * Input: version1 = "1.0.1", version2 = "1" |
| 22 | + * Output: 1 |
| 23 | + * |
| 24 | + * Example 3: |
| 25 | + * |
| 26 | + * Input: version1 = "7.5.2.4", version2 = "7.5.3" |
| 27 | + * Output: -1 |
| 28 | + * |
| 29 | + * Example 4: |
| 30 | + * |
| 31 | + * Input: version1 = "1.01", version2 = "1.001" |
| 32 | + * Output: 0 |
| 33 | + * Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1” |
| 34 | + * |
| 35 | + * Example 5: |
| 36 | + * |
| 37 | + * Input: version1 = "1.0", version2 = "1.0.0" |
| 38 | + * Output: 0 |
| 39 | + * Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0" |
| 40 | + * |
| 41 | + * |
| 42 | + * |
| 43 | + * Note: |
| 44 | + * <ol> |
| 45 | + * Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes. |
| 46 | + * Version strings do not start or end with dots, and they will not be two consecutive dots. |
| 47 | + * </ol> |
| 48 | + */ |
| 49 | +pub struct Solution {} |
| 50 | + |
| 51 | +// submission codes start here |
| 52 | + |
| 53 | +impl Solution { |
| 54 | + pub fn compare_version(version1: String, version2: String) -> i32 { |
| 55 | + let v1: Vec<&str> = version1.split('.').collect::<Vec<_>>(); |
| 56 | + let v2: Vec<&str> = version2.split('.').collect::<Vec<_>>(); |
| 57 | + let mut i = 0_usize; |
| 58 | + while i < v1.len() && i < v2.len() { |
| 59 | + let left = v1[i].parse::<u32>().unwrap(); |
| 60 | + let right = v2[i].parse::<u32>().unwrap(); |
| 61 | + if left > right { |
| 62 | + return 1; |
| 63 | + } else if left < right { |
| 64 | + return - 1; |
| 65 | + } |
| 66 | + i += 1; |
| 67 | + } |
| 68 | + while i < v1.len() { |
| 69 | + if v1[i].parse::<u32>().unwrap() > 0 { |
| 70 | + return 1; |
| 71 | + } |
| 72 | + i += 1; |
| 73 | + } |
| 74 | + while i < v2.len() { |
| 75 | + if v2[i].parse::<u32>().unwrap() > 0 { |
| 76 | + return -1; |
| 77 | + } |
| 78 | + i += 1; |
| 79 | + } |
| 80 | + return 0 |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// submission codes end |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_165() { |
| 92 | + assert_eq!(Solution::compare_version( "0.1".to_owned(), "1.1".to_owned()), -1); |
| 93 | + assert_eq!(Solution::compare_version( "1.0.1".to_owned(), "1".to_owned()), 1); |
| 94 | + assert_eq!(Solution::compare_version( "7.5.2.4".to_owned(), "7.5.3".to_owned()), -1); |
| 95 | + assert_eq!(Solution::compare_version( "1.01".to_owned(), "1.0001".to_owned()), 0); |
| 96 | + } |
| 97 | +} |
0 commit comments