当前位置:网站首页>38. arrangement of strings
38. arrangement of strings
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 38. Arrangement of strings
Ideas : to flash back
Definition
(i>0&&!isVisited[i-1]&&s[i]==s[i-1])
Ensure that repeated elimination , The prerequisite string needs to be sorted
technological process
- Sorting ensures that duplicates can be excluded
- Traversal string ,
- If the current character is not traversed or does not meet the repetition condition , Add characters
- Recursively call
- Pop up the added string
- Return results
class Solution {
public:
vector<string> permutation(string s) {
vector<string> ans;
string tmp;
vector<bool> isVisited(s.size());
sort(s.begin(),s.end());
permutation(ans,tmp,s,isVisited);
return ans;
}
void permutation(vector<string>& ans,string& tmp,string& s,vector<bool>& isVisited){
if(tmp.size()==s.size()){
ans.push_back(tmp);
return;
}
for(int i=0;i<s.size();++i){
if((i>0&&!isVisited[i-1]&&s[i]==s[i-1])||isVisited[i]) continue;
tmp.push_back(s[i]);
isVisited[i]=true;
permutation(ans,tmp,s,isVisited);
tmp.pop_back();
isVisited[i]=false;
}
}
};
Time complexity O(nxn!) String Full Permutation O(n!), Required per spread O(n) Time generation
Spatial complexity O(n) need O(n) Stack space
边栏推荐
- Map coordinate conversion of Baidu map API
- Computer network connected but unable to access the Internet
- cellular automaton
- How to generate provincial data from county-level data in ArcGIS?
- Nbiot module me3616 at command mqtt connecting thingsboard
- Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data
- LabVIEW關於TDMS和Binary存儲速度
- Microsoft announces that it will discontinue support for older versions of visual studio
- Pupanvr hardware and software board side development environment configuration (4)
- Difference between thread and task
猜你喜欢

Detailed usage of vim editor

Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering

4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)

Understanding of day16 array create query static and dynamic array array array performance in memory

Longest palindrome string

Simulation of array implementation stack

4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)

Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)

【cjson】根节点注意事项

Data processing and data set preparation
随机推荐
How to deploy PostgreSQL as a docker container
Longest palindrome string
It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
Thingsboard create RCP widget
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
Common MySQL date query
Esp32-who face detection
【cjson】根节点注意事项
Introduction to MMS memory optimization of Hisilicon MPP service
Detailed usage of vim editor
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
[backtracking based on bit operation] queen n problem 2
Stm32f4 ll library multi-channel ADC
Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature
Difference between thread and task
Advanced MySQL knowledge points (7)
Ubunt 20.04 uses CDROM or ISO as the installation source
Computer network connected but unable to access the Internet
Variables and data types