[Leetcode] 17. Letter Combinations of a Phone Number
来源:互联网 发布:武工队后勤部淘宝 编辑:程序博客网 时间:2024/06/05 14:47
Problem:
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
思路:
本题其实就是列举若干个集合间组合的所有情况。可以用迭代的方法完成。迭代的方法如下:令当前数字与之前的所有情况result list进行组合,然后再把组合得到的结果赋值给result list,实现迭代。
Solution:
class Solution(object): digitlist = [] count = 97 i = 0 for i in xrange(10): tmpl = [] if i == 0 or i == 1: digitlist.append(tmpl) continue elif i ==7 or i == 9: num = 4 else: num = 3 j = 0 for j in xrange(num): tmpl.append(chr(count)) count += 1 digitlist.append(tmpl) def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ if len(digits) == 0 : return [] result = [""] for s in digits: num = int(s) if num >=2 and num <= 9: tmplist = [] else: continue for d in self.digitlist[num]: for item in result: tmplist.append(item+d) result = tmplist return result
0 0
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- String、StringBuffer、与StringBuilder的区别
- 你还在为移动端选择器而捉急吗?【原创】
- 【DP入门】单调递增子序列(二)
- 69道Spring面试题和答案
- qt QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout. 错误
- [Leetcode] 17. Letter Combinations of a Phone Number
- [nginx] location定位
- 矩形内接任意多边形抠图(可以羽化边缘)
- js实现中介者模式
- android 几种单例模式的写法
- 随笔
- JVM参数配置大全
- java基本语法之 javadoc02
- JAVA操作Excel时文字自适应单元格的宽度设置方法