zoukankan      html  css  js  c++  java
  • String Permutation

    Problem

    感觉和上面的题又有点像, 给一个string, 里面不能有数字。 然后所有的大写字母和非字母符号不能动, 其他的小写字母可以随意动。 输出所有的可能。
    e.g. input Oh my-god!
       output Om hd-goy! Oy hm-dog! 等等。。

    Solution

     1 public static List<String> permutate(String str) {
     2     List<String> res = new ArrayList<String>();
     3     if(str == null || str.length() == 0) return res;
     4     
     5     StringBuffer sb = new StringBuffer(str);
     6     int[] idx = new int[str.length()];
     7     
     8     int low = 'a', high = 'z';
     9     int start = 0;
    10     for(int i=0; i<str.length(); i++) {
    11         if(str.charAt(i) <= high && str.charAt(i) >= low) {
    12             idx[start] = i;
    13             start++;
    14         }
    15     }
    16     
    17     System.out.println(start);
    18 
    19     boolean[] visited = new boolean[start];
    20     helper(str, res, sb, idx, visited, 0, start);
    21     return res;
    22 }
    23 
    24 public static void helper(String str, List<String> res, StringBuffer sb, int[] idx, boolean[] visited, int pos, int end) {
    25     if(pos == end) {
    26         res.add(new StringBuffer(sb).toString());
    27         return;
    28     }
    29             
    30     for(int i=0; i<end; i++) {
    31         if(visited[i] == false) {
    32             visited[i] = true;
    33             sb.setCharAt(idx[pos], str.charAt(idx[i]));
    34             helper(str, res, sb, idx, visited, pos+1, end);
    35             visited[i] = false;
    36         }
    37     }
    38 }
  • 相关阅读:
    Js获取URL中的QueryStirng字符串
    GridView分页操作
    c语言string的函数
    char *a 与char a[] 的区别
    htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
    nfs 原理详解
    NFS和mount常用参数详解 本文目录
    网络安全过滤软件
    SNMP协议详解
    win7开启snmp服务
  • 原文地址:https://www.cnblogs.com/superbo/p/4112093.html
Copyright © 2011-2022 走看看