NC190. 字符串的全部子序列
描述
示例1
输入:
"ab"
输出:
["","a","ab","b"]
说明:
返回["","b","a","ab"]也是可以的,视为正确,顺序不唯一示例2
输入:
"dbcq"
输出:
["","b","bc","bcq","bq","c","cq","d","db","dbc","dbcq","dbq","dc","dcq","dq","q"]
示例3
输入:
"aab"
输出:
["","a","aa","aab","ab","b"]
说明:
返回的字符串数组里面不能存在"ab","ab"这样2个相同的子序列C++ 解法, 执行用时: 11ms, 内存消耗: 3200KB, 提交时间: 2022-06-07
class Solution { public: void bt(string& s,int startIndex,vector<string>& r,string& temp) { r.emplace_back(temp); int used[26]={0}; for(int i=startIndex;i<s.size();i++) { if(used[s[i]-'a']==1) continue; temp.push_back(s[i]); used[s[i]-'a']=1; bt(s,i+1,r,temp); temp.pop_back(); } } vector<string> generatePermutation(string s) { vector<string>r; string temp; bt(s,0,r,temp); return r; } };
C++ 解法, 执行用时: 13ms, 内存消耗: 3240KB, 提交时间: 2022-08-06
class Solution { public: int n; void dfs(vector<string>&re,string &s,string &path,int st) { re.push_back(path); if(st==n){return;} int mp[128]; memset(mp,0,sizeof(mp)); for(int i=st;i<n;i++) { if(mp[s[i]]==0) { mp[s[i]]=1; path.push_back(s[i]); dfs(re,s,path,i+1); path.pop_back(); } } } vector<string> generatePermutation(string s) { n = s.size(); vector<string> re; //vector<int> visall(n); string path=""; dfs(re,s,path,0); return re; } };
C++ 解法, 执行用时: 13ms, 内存消耗: 3336KB, 提交时间: 2022-06-01
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: void bt(string& s,int startIndex,vector<string>& r,string& temp) { r.emplace_back(temp); int used[26]={0}; for(int i=startIndex;i<s.size();i++) { if(used[s[i]-'a']==1) continue; temp.push_back(s[i]); used[s[i]-'a']=1; bt(s,i+1,r,temp); temp.pop_back(); } } vector<string> generatePermutation(string s) { vector<string>r; string temp; bt(s,0,r,temp); return r; } };
C++ 解法, 执行用时: 14ms, 内存消耗: 3308KB, 提交时间: 2022-08-03
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串vector */ vector<string> generatePermutation(string s) { vector<string> ans; string tmp; helper(ans, s, 0, tmp); return ans; } void helper(vector<string>& ans, const string& s, const int& index, string tmp) { ans.emplace_back(tmp); int used[26] = {0}; for (int i = index; i < s.size(); ++i) { if (used[s[i] - 'a'] == 1) continue; tmp.push_back(s[i]); used[s[i] - 'a'] = 1; helper(ans, s, i + 1, tmp); tmp.pop_back(); } } };
C++ 解法, 执行用时: 14ms, 内存消耗: 5392KB, 提交时间: 2022-05-17
class Solution { public: vector<string> result; string path; void backtracking(string &s,int startIndex){ result.push_back(path); int used[26]={0};//用数组来对本层去重 for(int i=startIndex;i<s.size();i++){ if(used[s[i]-'a']==1){ continue; } path.push_back(s[i]); used[s[i]-'a']=1; backtracking(s, i+1); path.pop_back(); } } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串vector */ vector<string> generatePermutation(string s) { // write code here result.clear(); path.clear(); //sort(s.begin(),s.end()); backtracking(s,0); return result; } };