JZ27 字符串的排列

字符串的排列

https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&&tqId=11180&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

排列问题
for循环 每次选一个字符 然后递归

import java.util.*;
public class Solution {
    ArrayList<String> res = new ArrayList<>();
    char[] c;
    public ArrayList<String> Permutation(String str) {
        c = str.toCharArray();
        dfs(0);
        return res;
    }
    public void dfs(int x){
        if(x == c.length-1) {res.add(new String(c)); return;}
        HashSet<Character> set = new HashSet<>();
        for(int i=x;i<c.length;i++){
            if(!set.contains(c[i])){
                set.add(c[i]);
                swap(i,x);
                dfs(x+1);
                swap(i,x);
            }
        }
    }
    public void swap(int i, int j){
        char temp = c[i];
        c[i]=c[j];
        c[j]=temp;
    }

}
全部评论

相关推荐

10-01 09:50
门头沟学院 Java
肖先生~:这个人真的很好,点赞
点赞 评论 收藏
分享
09-14 20:51
四川大学 Java
慢热的鲸鱼在学习:985加粗就行了,第二个项目来不及准备也没事,省的写了问你你还不会。你只需准备面试八股和项目场景,剩下的交给985。即使面不过也没事,面试经验是最重要的,你现在不缺时间
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务