Given a string containing digits from 2-9
inclusive, 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. Note that 1 does not map to any letters.
Example:
Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Special Point:
C11 is an effective tool.From now on I need to get familiar with C11.
Some usage in for loop need to be declare:
1.All These instance only want to declare one thing:
if you want to change value during C11 for loop,you need to use atuo&
Source of theory:
https://blog.youkuaiyun.com/hailong0715/article/details/54172848
std::vector<int> vec {1,2,3,4,5,6,7,8,9,10}; cout << "修改前" << endl; for (auto n :vec) std::cout << n++; cout << endl; cout << "修改后" << endl; for (auto j : vec) std::cout << j;
修改前 12345678910 修改后 12345678910
std::vector<int> vec {1,2,3,4,5,6,7,8,9,10}; cout << "修改前" << endl; for (auto& n :vec) std::cout << n++; cout << endl; cout << "修改后" << endl; for (auto j : vec) std::cout << j;
修改前 12345678910 修改后 234567891011
In this code swap means swap value between two vector
This way is BFS.Is easy than DFS
TIME COMPLEXITY:O(4^n)
SPACE COMPLEXITY:O(2*4^n) because it need two vector loop data through each other
#include<iostream> #include<string> #include<stdio.h> #include<string.h> #include<iomanip> #include<vector> #include<list> #include<queue> #include<algorithm> #include<map> using namespace std; class Solution{ public: vector<string> letterCombinations(string input) { if(input.empty()) return {}; vector<vector<char>> temp(10); temp[0]={}; temp[1]={}; temp[2]={'a','b','c'}; temp[3]={'d','e','f'}; temp[4]={'g','h','i'}; temp[5]={'j','k','l'}; temp[6]={'m','n','o'}; temp[7]={'p','q','r','s'}; temp[8]={'t','u','v'}; temp[9]={'w','x','y','z'}; vector<string> res{""}; for(char in:input) { vector<string> bridge; for(const string& s:res) for(char tp:temp[in-'0']) bridge.push_back(s+tp); res.swap(bridge); } return res; } }; int main() { vector<string> res; string input; cin>>input; Solution s; res=s.letterCombinations(input); vector<string>::iterator iter=res.begin(); for(iter;iter!=res.end();++iter) { cout<<*iter<<endl; } return 0; }
18
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b + c + d = target
? Find all unique quadruplets in the array which gives the sum of target
.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
class Solution { public: vector<vector<int>> fourSum(vector<int> &nums, int target) { vector<vector<int>> res; int n = nums.size(); sort(nums.begin(), nums.end()); for (int i = 0; i < n - 3; ++i) { if (i > 0 && nums[i] == nums[i - 1]) continue; for (int j = i + 1; j < n - 2; ++j) { if (j > i + 1 && nums[j] == nums[j - 1]) continue; int left = j + 1, right = n - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { vector<int> out{nums[i], nums[j], nums[left], nums[right]}; res.push_back(out); while (left < right && nums[left] == nums[left + 1]) ++left; while (left < right && nums[right] == nums[right - 1]) --right; ++left; --right; } else if (sum < target) ++left; else --right; } } } return res; } };