当前位置:网站首页>Sword finger offer 38 Arrangement of strings
Sword finger offer 38 Arrangement of strings
2022-06-29 23:16:00 【GRT has to keep working hard】


First, fix the first position n Kind of , Then the second one has (n - 1) Kind of , By analogy .
When there are duplicate characters in the string , There will also be repeated schemes in the arrangement . To eliminate duplicate schemes , When fixing a character , Guarantee “ Each character is fixed only once in this bit ” , That is, when repeated characters are encountered, they are not exchanged , Just skip . from DFS Perspective , This operation is called “ prune ” .
class Solution {
List<String> res = new ArrayList<>();
char[] arr;
public String[] permutation(String s) {
arr = s.toCharArray();
dfs(0);
return res.toArray(new String[res.size()]);
}
// Backtracking function
void dfs(int x){
if(x == arr.length-1){
res.add(String.valueOf(arr));
return;
}
Set<Character>set = new HashSet<>();
for(int i = x; i<arr.length; i++){
if(set.contains(arr[i])){
continue;}// Repeated pruning
set.add(arr[i]);
swap(i,x);
dfs(x+1);
swap(i,x);
}
}
// Exchange function
void swap(int a,int b){
char ch = arr[a];
arr[a] = arr[b];
arr[b] = ch;
}
}
The recursive aspect of intermediate exchange is not very clear .
边栏推荐
- constexpr 函数
- The details of industry are all made by money and time
- Taishan Office Technology Lecture: all elements in a row have the same height
- leetcode 416. Partition equal subset sum partition equal subset sum (medium)
- 论文阅读《Large-Scale Direct SLAM with Stereo Cameras》
- Status acquisition and control system of on-site express cabinet
- grpc的开发详解
- An in-depth analysis of the election mechanism in kubernetes
- How to use filters in jfinal to monitor Druid for SQL execution?
- Basic use of Nacos configuration center
猜你喜欢

剑指 Offer 38. 字符串的排列

Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04

Deep parsing of kubernetes controller runtime

Node data collection and remote flooding transmission of label information

深入解析kubernetes controller-runtime
详细聊聊MySQL中auto_increment有什么作用
Evolution from stand-alone to distributed database storage system

语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】

收藏!这些提高程序员生产力的工具你用过吗?

Vs2013 how to make the program run on other computers
随机推荐
Shell -- text processing command
Efficient implementation of dynamiccast with template function and specialization function
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
架构实战营模块 5 作业
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
剑指 Offer 38. 字符串的排列
Mysql database: storage engine
Discussion on distributed unique ID generation scheme
Ansible automatic operation and maintenance
What if MySQL fails to store emoticons
字节云数据库未来方向的探索与实践
[cooking record] - hot and sour cabbage
语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】
How ZABBIX 5.0 adds esxi6.7 to monitoring
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
Does rapid software delivery really need to be at the cost of security?
[proteus simulation] digital tube display of stepping motor speed
SYSTEMd debugging
Regular expressions: characters (2)
Polymorphism of laravel association model