-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-052.cpp
76 lines (71 loc) · 2.06 KB
/
Day-052.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<vector>
using namespace std;
void solve(string digit ,string output ,int index ,vector<string>& ans ,string mapping[]){
// base case
if(index >= digit.length()){
ans.push_back(output);
return;
}
int number = digit[index]-'0';
string value = mapping[number];
for(int i =0;i <value.length();i++){
output.push_back(value[i]);
solve(digit,output,index+1,ans,mapping);
output.pop_back();
}
}
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.length()==0){
return ans;
}
string output;
int index =0;
string mapping[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
solve(digits,output,index,ans,mapping);
return ans;
}
class Solution {
void solve(vector<int>nums, vector<int> output, int index, vector<vector<int>> & ans){
if(index >= nums.size()){
ans.push_back(output);
return;
}
//exclude
solve(nums,output,index+1,ans);
//include
output.push_back(nums[index]);
solve(nums,output,index+1,ans);
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> output;
int index = 0;
solve(nums,output,index,ans);
return ans;
}
};
class Solution {
void solve(vector<int> nums,vector<vector<int>>& ans, int index){
// base case
if(index >= nums.size()){
ans.push_back(nums);
return;
}
for(int j = index ; j < nums.size();j++){
swap(nums[index],nums[j]);
solve(nums,ans,index+1);
// backtrack
swap(nums[index],nums[j]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
int index = 0;
solve(nums,ans,index);
return ans;
}
};