简体   繁体   English

字符串排列

[英]String Permutations

I was recently trying to write a script that print out all the permutations of a word in Java. 我最近试图编写一个脚本,打印出Java中所有单词的排列。 For some reason it only prints out one. 由于某种原因,它只打印出一个。 I just can't figure it out! 我只是想不出来!

import java.util.*;

public class AllPermutations {

    ArrayList<String> letters = new ArrayList<String>();
    public void main(){
        letters.add("H");
        letters.add("a");
        letters.add("s");
        permutate("",letters);
    }

    public void permutate(String word, ArrayList<String> lettersLeft){
        if(lettersLeft.size()==0){
            System.out.println(word);
        }else{
            for(int i=0;i<lettersLeft.size();i++){
                String newWord = new String();
                newWord = word+lettersLeft.get(i);
                lettersLeft.remove(i);
                permutate(newWord, lettersLeft);
            }
        }
    }
}

You need to add the letter you have removed back to the lettersLeft list 您需要将已删除的字母添加回lettersLeft列表

public void permutate(String word, ArrayList<String> lettersLeft){
    if(lettersLeft.size()==0){
        System.out.println(word);
    }else{
        for(int i=0;i<lettersLeft.size();i++){
            String temp = lettersLeft.remove(i);
            String  newWord = word+temp;
            permutate(newWord, lettersLeft);
            lettersLeft.add(i, temp);
        }
    }
}

I haven't tested it, but I think it should work. 我没有测试它,但我认为它应该工作。

The problem is that Java/you are passing by reference, not copy (ArrayList). 问题是Java /您通过引用传递,而不是复制(ArrayList)。 Therefore once you reach the bottom of your recursion tree, lettersLeft will contain 0 elements, and once you go back up, it will still have 0 elements. 因此,一旦到达递归树的底部,lettersLeft将包含0个元素,一旦你返回,它仍将有0个元素。

As a side note, StringBuilder/StringBuffer is better at doing string permutation task, since String is immutable, therefore you are wasting a lot of resource creating new Strings, n! 作为旁注,StringBuilder / StringBuffer更擅长执行字符串置换任务,因为String是不可变的,因此你浪费了很多资源来创建新的字符串,n! to be exact. 确切地说。 The difference between the two StringBuilder/Buffer is up to you to discover. 两个StringBuilder / Buffer之间的区别由你来发现。

The reason for that is lettersLeft is being passed by reference always. 原因是lettersLeft总是通过引用传递。 Once you are removing a letter from lettersLeft, it is being permanently removed. 一旦你从lettersLeft删除一封信,它就会被永久删除。 So for the first iteration you have "HAS" printed out. 因此,对于第一次迭代,您打印出“HAS”。 once that finishes, the recursion algorithm backs up a level to make the second iteration, but what do you know?? 一旦完成,递归算法会备份一个级别来进行第二次迭代,但是你知道什么? lettersLeft is empty. lettersLeft是空的。 so it terminates without passing by the if statement causing it not to get another word or permutation. 所以它终止而不通过if语句导致它不会得到另一个词或排列。 In order to resolve this, create a local copy, just like you did with newWord. 要解决此问题,请创建本地副本,就像使用newWord一样。 Hope that helps. 希望有所帮助。

In this case you are removing the letters from the Arraylist and it gets empty till it reaches the end of first word.. Then after that list size is always zero... Add the removed letter back to the list ........... 在这种情况下,您将删除Arraylist的字母,它将变为空,直到它到达第一个单词的结尾。然后在该列表大小始终为零之后... Add the removed letter back to the list ...... .....

I would recommend you to use the below link and find good examples of String Permutations as there are both memory efficient and space efficient solutions of String permutations... recommend你使用下面的链接,找到String Permutations的好例子,因为有内存效率和空间有效的String排列解决方案......

http://www.codingeek.com/java/strings/find-all-possible-permutations-of-string-using-recursive-method/ http://www.codingeek.com/java/strings/find-all-possible-permutations-of-string-using-recursive-method/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM