zoukankan      html  css  js  c++  java
  • 字符串的排列

    题目描述

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

    输入描述:

    输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

     1 import java.util.*;
     2 public class Solution {
     3     Set<String> s;
     4     public void dfs(ArrayList<String> ans, String temp, String str, boolean []vis) {
     5         if (temp.length() == str.length()) {
     6             if (!s.contains(temp)) {
     7                 ans.add(temp);
     8                 s.add(temp);
     9             }
    10             return;
    11         }
    12         int n = str.length();
    13         for (int i = 0; i < n; ++i) {
    14             if (vis[i]) continue;
    15             vis[i] = true;
    16             dfs(ans, temp + str.substring(i, i + 1), str, vis);
    17             vis[i] = false;
    18         }
    19     }
    20     public ArrayList<String> Permutation(String str) {
    21         int n = str.length();
    22         ArrayList<String> ans = new ArrayList<>();
    23         if (n == 0) return ans;
    24         s = new HashSet<>();
    25         boolean []vis = new boolean [n + 1];
    26         String temp = new String();
    27         dfs(ans, temp, str, vis);
    28         return ans;
    29     }
    30 }
  • 相关阅读:
    python常用运维脚本实例
    数据库优化方案整理
    Python第一章概述与环境安装
    九阴真经
    常用的kubectl命令
    第7篇管理存储资源
    第8篇NFS PersistentVolume
    第10篇用 ConfigMap 管理配置
    第11篇Kubernetes部署微服务电商平台
    第12篇Kubernetes 监控
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/12344392.html
Copyright © 2011-2022 走看看