[LeetCode][Python3] 17. Letter Combinations of a Phone Number
2018. 11. 9. 01:40 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/letter-combinations-of-a-phone-number
My Solution :
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
self.ans = []
self.conv = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
self.combine('', digits)
return self.ans
def combine(self, prefix, remain):
if remain:
for c in self.conv[remain[0]]:
self.combine(prefix + c, remain[1:])
elif prefix:
self.ans.append(prefix)
Comment :
뒤로 갈 수록 경우의 수가 많아지는 문제라서 직관적으로 recursion으로 접근하였다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 36. Valid Sudoku (0) | 2018.11.11 |
---|---|
[LeetCode][Python3] 34. Find First and Last Position of Element in Sorted Array (0) | 2018.11.11 |
[LeetCode][Python3] 22. Generate Parentheses (0) | 2018.11.10 |
[LeetCode][Python3] 19. Remove Nth Node From End of List (0) | 2018.11.09 |
[LeetCode][Python3] 11. Container With Most Water (0) | 2018.11.07 |
[LeetCode][Python3] 108. Convert Sorted Array to Binary Search Tree (0) | 2018.11.06 |
[LeetCode][Python3] 387. First Unique Character in a String (0) | 2018.11.05 |
[LeetCode][Python3] 350. Intersection of Two Arrays II (0) | 2018.11.05 |
최근에 달린 댓글 최근에 달린 댓글