문제 URL : leetcode.com/problems/letter-combinations-of-a-phone-number/
Letter Combinations of a Phone Number - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 해석 : 2~9까지 숫자중 아무숫자를 눌러서 영문으로 바꿀수있는 조합의 수를 구하라는 문제입니다.
최대 4개 까지 누를수 있고 옛날 핸드폰 문자 형식처럼 이미지가 주어졌기 때문에 바로 감이 오실겁니다.
문제 접근법 : 만들 수있는 조합을 다구해야하기 때문에 dfs로 n에 해당하는 깊이까지 차례대로 구하면 됩니다.
영어 문제지만 쉬운문제이니 한번 풀어보시길
소스코드 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
//By 콩순이냉장고
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
unordered_map<char, string> Map = {
{ '2', "abc" }, { '3', "def" }, { '4', "ghi" }, { '5', "jkl" },
{'6', "mno" }, { '7', "pqrs" }, {'8', "tuv" }, { '9', "wxyz" }
};
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
dfs(digits, 0, result);
return result;
}
void dfs(string &digits,int idx, vector<string> &v, string result=""){
if (idx >= digits.size())
{
if (result == "")
return;
v.push_back(result);
return;
}
for (int i = 0; i < Map[digits[idx]].size(); i++){
dfs(digits, idx + 1, v, result + Map[digits[idx]][i]);
}
}
};
|
cs |
영어공부도 시작할겸 이렇게 영어로된 코딩문제를 푸는것도 좋지 않을까 생각됩니다.
궁금한점 혹은 모르는점 언제든지 댓글을 이용해주시면 감사합니다.
'LeetCode' 카테고리의 다른 글
LeetCode 38 Count and Say (0) | 2021.07.15 |
---|---|
LeetCode ZigZag Conversion (0) | 2021.07.14 |
LeetCode 28 Implement strStr() (0) | 2021.07.06 |
LeetCode 59 Spiral Matrix II (0) | 2021.02.10 |
LeetCode #100 Same Tree (0) | 2021.01.06 |