思路
一个一个遍历就好了,不难。
代码
class Solution {fun letterCombinations(digits: String): List<String> {if (digits.isEmpty()) return emptyList()val map = mapOf('2' to arrayOf('a', 'b', 'c'),'3' to arrayOf('d', 'e', 'f'),'4' to arrayOf('g', 'h', 'i'),'5' to arrayOf('j', 'k', 'l'),'6' to arrayOf('m', 'n', 'o'),'7' to arrayOf('p', 'q', 'r', 's'),'8' to arrayOf('t', 'u', 'v'),'9' to arrayOf('w', 'x', 'y', 'z'))var result = mutableListOf("")var index = 0while (index < digits.length) {var nowList = mutableListOf<String>()val char = digits[index]result.forEach{str ->map[char]?.forEach {c ->nowList.add(str + c)}}index++result = nowList}return result}}
