|
| 1 | +/** |
| 2 | + * [17] 电话号码的字母组合 |
| 3 | + * |
| 4 | + * 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 |
| 5 | + * 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 |
| 6 | + * <img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/11/09/200px-telephone-keypad2svg.png" style="width: 200px;" /> |
| 7 | + * |
| 8 | + * 示例 1: |
| 9 | + * |
| 10 | + * 输入:digits = "23" |
| 11 | + * 输出:["ad","ae","af","bd","be","bf","cd","ce","cf"] |
| 12 | + * |
| 13 | + * 示例 2: |
| 14 | + * |
| 15 | + * 输入:digits = "" |
| 16 | + * 输出:[] |
| 17 | + * |
| 18 | + * 示例 3: |
| 19 | + * |
| 20 | + * 输入:digits = "2" |
| 21 | + * 输出:["a","b","c"] |
| 22 | + * |
| 23 | + * |
| 24 | + * 提示: |
| 25 | + * |
| 26 | + * 0 <= digits.length <= 4 |
| 27 | + * digits[i] 是范围 ['2', '9'] 的一个数字。 |
| 28 | + * |
| 29 | + */ |
| 30 | +pub struct Solution {} |
| 31 | + |
| 32 | +// problem: https://leetcode.cn/problems/letter-combinations-of-a-phone-number/ |
| 33 | +// discuss: https://leetcode.cn/problems/letter-combinations-of-a-phone-number/discuss/?currentPage=1&orderBy=most_votes&query= |
| 34 | + |
| 35 | +// submission codes start here |
| 36 | + |
| 37 | +impl Solution { |
| 38 | + pub fn letter_combinations(digits: String) -> Vec<String> { |
| 39 | + if digits.len() == 0 { |
| 40 | + return vec![]; |
| 41 | + } |
| 42 | + return Self::permutation(&digits); |
| 43 | + } |
| 44 | + |
| 45 | + fn permutation(digits: &str) -> Vec<String> { |
| 46 | + if digits.len() == 1 { |
| 47 | + let c = digits.as_bytes()[0]; |
| 48 | + if c == b'9' { |
| 49 | + return vec![ |
| 50 | + "w".to_string(), |
| 51 | + "x".to_string(), |
| 52 | + "y".to_string(), |
| 53 | + "z".to_string(), |
| 54 | + ]; |
| 55 | + } else if c == b'7' { |
| 56 | + return vec![ |
| 57 | + "p".to_string(), |
| 58 | + "q".to_string(), |
| 59 | + "r".to_string(), |
| 60 | + "s".to_string(), |
| 61 | + ]; |
| 62 | + } else if c == b'8' { |
| 63 | + return vec!["t".to_string(), "u".to_string(), "v".to_string()]; |
| 64 | + } |
| 65 | + let c = (c - b'2') * 3 + b'a'; |
| 66 | + return vec![ |
| 67 | + String::from_utf8(vec![c]).unwrap(), |
| 68 | + String::from_utf8(vec![c + 1]).unwrap(), |
| 69 | + String::from_utf8(vec![c + 2]).unwrap(), |
| 70 | + ]; |
| 71 | + } |
| 72 | + let vec1 = Self::permutation(&digits[0..1]); |
| 73 | + let vec2 = Self::permutation(&digits[1..]); |
| 74 | + let mut res = Vec::new(); |
| 75 | + for x in vec1.iter() { |
| 76 | + for y in vec2.iter() { |
| 77 | + res.push(format!("{}{}", x, y)); |
| 78 | + } |
| 79 | + } |
| 80 | + res |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// submission codes end |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_17() { |
| 92 | + assert_eq!( |
| 93 | + Solution::letter_combinations("2".to_string()), |
| 94 | + vec!["a", "b", "c"] |
| 95 | + ); |
| 96 | + assert_eq!( |
| 97 | + Solution::letter_combinations("3".to_string()), |
| 98 | + vec!["d", "e", "f"] |
| 99 | + ); |
| 100 | + assert_eq!( |
| 101 | + Solution::letter_combinations("9".to_string()), |
| 102 | + vec!["w", "x", "y", "z"] |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + Solution::letter_combinations("23".to_string()), |
| 106 | + vec!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] |
| 107 | + ); |
| 108 | + let emptyvec: Vec<String> = vec![]; |
| 109 | + assert_eq!( |
| 110 | + Solution::letter_combinations("".to_string()), |
| 111 | + emptyvec |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments