当前位置:网站首页>剑指 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;
}
}
中间交换递归方面不太明确。
边栏推荐
- What if MySQL fails to store emoticons
- Can you be a coder if you don't learn English well? Stop worrying and learn it first
- Processing of error b6267342 reported by AIX small machine in production environment
- math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
- Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
- Design of Distributed Message Oriented Middleware
- MySQL lock common knowledge points & summary of interview questions
- 开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例
- Ansible自动化运维
猜你喜欢

Code sharing for making and developing small programs on the dating platform

What if MySQL fails to store emoticons

Problem solving metauniverse, multi communication scheme in online games

#第三天

分布式消息中间件设计

VS无法定位程序输入点于动态链接库

Wireshark data analysis and forensics information pacapng

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践

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

Digital tracking analysis of insurance services in the first quarter of 2022
随机推荐
Use the leader election mechanism in kubernetes to complete your own ha application
稳!上千微服务接入 Zadig 的最佳姿势(Helm Chart 篇)
Hezhou air32f103cbt6 development board hands-on Report
What if MySQL fails to store emoticons
Efficient implementation of dynamiccast with template function and specialization function
Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
5-2web application vulnerability scanning
Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
Day9 ---- 用户注册与登录
The development of grpc
Static keyword continuation, inheritance, rewrite, polymorphism
合宙AIR32F103CBT6开发板上手报告
Vs cannot locate program input point to DLL
《天天数学》连载54:二月二十三日
MySQL lock common knowledge points & summary of interview questions
Problem solving metauniverse, multi communication scheme in online games
Digital tracking analysis of insurance services in the first quarter of 2022
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
2022年第一季度保险服务数字化跟踪分析