当前位置:网站首页>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 .
边栏推荐
- 字节云数据库未来方向的探索与实践
- STM32基础知识点
- Efficient implementation of dynamiccast with template function and specialization function
- 0. grpc environment setup
- Wechat applet: big red festive UI guessing lantern riddles is also called guessing character riddles
- Static keyword continuation, inheritance, rewrite, polymorphism
- Wechat applet: (update) cloud development wechat group contacts
- leetcode 416. Partition equal subset sum partition equal subset sum (medium)
- Node data collection and remote flooding transmission of label information
- Evolution from stand-alone to distributed database storage system
猜你喜欢

Deep parsing of kubernetes controller runtime

Processing of error b6267342 reported by AIX small machine in production environment

How to solve the problem that the computer time is not automatically updated after proofreading

Problem solving metauniverse, multi communication scheme in online games

C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数

Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently
![The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]](/img/96/3e634c173c96082881286ba402a067.png)
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]

Node data collection and remote flooding transmission of label information

地方/园区如何做好产业分析?

深入解析kubernetes controller-runtime
随机推荐
众昂矿业:萤石助力氟产业锂电建设发展
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
架构实战营模块 5 作业
2022年PMP项目管理考试敏捷知识点(5)
redis客户端
写论文工具:LaTex在线网站
Problem solving metauniverse, multi communication scheme in online games
Mysql database: read write separation
Wechat applet: (update) cloud development wechat group contacts
nrm详解
laravel 关联模型 多态关系
Mysql database: use the show profile command to analyze performance
80-Redis详解
十大券商:“推土机行情”再现
What if MySQL fails to store emoticons
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型
Can you be a coder if you don't learn English well? Stop worrying and learn it first
大学里遗憾的事,希望你无怨也无悔
Wireshark data analysis and forensics information pacapng