当前位置:网站首页>剑指 Offer 38. 字符串的排列
剑指 Offer 38. 字符串的排列
2022-06-29 22:34:00 【grt要一直一直努力呀】


先固定第一位有n种,然后第二位有(n - 1)种,依次类推。
当字符串中存在重复字符时,排列中也会出现重复的方案。为排除重复方案,需在固定某位字符时,保证 “每种字符只在此位固定一次” ,即遇到重复字符时不交换,直接跳过。从 DFS 角度看,此操作称为 “剪枝” 。
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()]);
}
//回溯函数
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;}//重复的剪枝
set.add(arr[i]);
swap(i,x);
dfs(x+1);
swap(i,x);
}
}
//交换函数
void swap(int a,int b){
char ch = arr[a];
arr[a] = arr[b];
arr[b] = ch;
}
}
中间交换递归方面不太明确。
边栏推荐
- AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
- static关键字续、继承、重写、多态
- Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures
- 从零实现深度学习框架——RNN从理论到实战【实战】
- Advanced use of the optional class
- Vs cannot locate program input point to DLL
- Use the leader election mechanism in kubernetes to complete your own ha application
- 啃下大骨头——排序(一)
- Mysql database: read write separation
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
猜你喜欢

交友平台小程序制作开发代码分享

Qt中使用QDomDocument和QDomnode来读取xml
![[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance](/img/72/214ee6d3842f393164cc93bb387926.png)
[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance

解题元宇宙,网络游戏中的多元通信方案

Discussion on distributed unique ID generation scheme

还天天熬夜加班做报表?其实你根本不懂如何高效做报表
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】

在线文本数字识别列表求和工具

Processing of error b6267342 reported by AIX small machine in production environment
随机推荐
5-1 system vulnerability scanning
Kubernetes architecture that novices must know
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
读书郎上市背后隐忧:业绩下滑明显,市场地位较靠后,竞争力存疑
Is it safe to open a stock account? Shanghai stock account opening.
Day9 - user registration and login
If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview
How ZABBIX 5.0 adds esxi6.7 to monitoring
Mysql database: use the show profile command to analyze performance
Can the flick CDC be used for incremental synchronization from Oracle to MySQL
Processing of error b6267342 reported by AIX small machine in production environment
从零实现深度学习框架——LSTM从理论到实战【理论】
Phpspreadsheet reading and writing Excel files
leetcode 416. Partition equal subset sum partition equal subset sum (medium)
An in-depth analysis of the election mechanism in kubernetes
正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
uniapp复制内容到剪贴板
5 - 1 Analyse de vulnérabilité du système
低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable