leetcode 第17題 分析 char*和string相互轉化ios
default (1) string(); copy (2) string (const string& str); substring (3) string (const string& str, size_t pos, size_t len = npos); from c-string (4) string (const char* s); from buffer (5) string (const char* s, size_t n); fill (6) string (size_t n, char c); range (7) template <class InputIterator> //vector<string> v; //str(v.begin(),v.end()); string (InputIterator first, InputIterator last); initializer list (8) string (initializer_list<char> il); move (9) string (string&& str) noexcept;
#include <iostream> #include <vector> #include <string> using namespace std; class Solution { public: vector<string> letterCombinations(string digits) { vector< vector<char> >phone{{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}}; if(digits.empty()) return vector<string>{}; vector<string> resNext=letterCombinations(digits.substr(1)); int curNum=digits[0]-'0'-2; vector<char> vc(phone[curNum]); vector<string> resCur; for(auto &each:vc){ vector<string> temp(resNext); //這一行不能丟,temp爲空的時候 char->string if(temp.empty()){string s(&each,1);resCur.push_back(s);continue;} for(auto& str:temp){ str=each+str; } resCur.insert(resCur.end(),temp.begin(),temp.end()); } return resCur; } }; int main(){ Solution sol; string digits="23"; vector<string> res=sol.letterCombinations(digits); for(int i=0;i<res.size();++i){ cout<<res[i]<<endl; } }