当前位置:网站首页>剑指 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;
}
}
中间交换递归方面不太明确。
边栏推荐
- Online text digit recognition list summation tool
- Polymorphism of laravel association model
- Live broadcast platform development, enter the visual area to execute animation, dynamic effects and add style class names
- 解题元宇宙,网络游戏中的多元通信方案
- If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- Design of Distributed Message Oriented Middleware
- SYSTEMd debugging
- Number theory - division and blocking
- Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
猜你喜欢

#第三天
![Realizing deep learning framework from zero -- RNN from theory to practice [practice]](/img/a0/d64b69dec4a8f3a3dbc2eb47df9372.png)
Realizing deep learning framework from zero -- RNN from theory to practice [practice]

Vs cannot locate program input point to DLL

深入解析kubernetes controller-runtime

Online text digit recognition list summation tool

触摸按键与按键控制对应的LED状态翻转

Digital tracking analysis of insurance services in the first quarter of 2022

Detailed description of gaussdb (DWS) complex and diverse resource load management methods

2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合

IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products
随机推荐
How ZABBIX 5.0 adds esxi6.7 to monitoring
2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
【Proteus仿真】步进电机转速数码管显示
How can the local / park do a good job in industrial analysis?
PhpSpreadsheet读写Excel文件
Unity Pac Man games, maze implementation
laravel 关联模型 多态关系
Laravel 创建自己的 Facade 扩展 geoip 根据 IP 获取国家、地域、城市信息
[proteus simulation] digital tube display of stepping motor speed
5-1系统漏洞扫描
math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
#第三天
5-2Web应用程序漏洞扫描
从零实现深度学习框架——LSTM从理论到实战【理论】
uniapp复制内容到剪贴板
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
Optional类的高级使用
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
英语没学好到底能不能做coder,别再纠结了先学起来