如何在java中打印0,1,2的词典排列

时间:2015-08-26 07:14:09

标签: java

如何打印给出整数值的字典排列。例如,如果我给出012那么输出应该是012 021 102 120 201 210。

我试图实现这一目标,

package TEstingHere;

public class LexicographicPermutations {

    private static int permutationsFound = 0;

       public static void main(String[] args) {
          permuteTo("012", "");
       }

       private static void permuteTo(String s, String chosen) {
          if (s.length() == 0) {
             permutationsFound++;
             if (permutationsFound == 1000000) {
                System.out.println(chosen);
             }
          } else if (permutationsFound <= 1000000) {
             for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                String rest = s.substring(0, i) + s.substring(i + 1);
               // System.out.println(rest);
                permuteTo(rest, chosen + ch);
             }
             System.out.println(chosen);
          }
       }


}

但这段代码不符合我的要求。

有人告诉我该怎么做?

3 个答案:

答案 0 :(得分:1)

如果我们只需要打印数字的词典排列,我会实现以下算法:来源wiki

以下算法在给定排列后按字典顺序生成下一个排列。它就地改变了给定的排列。

Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
Find the largest index l greater than k such that a[k] < a[l].
Swap the value of a[k] with that of a[l].
Reverse the sequence from a[k + 1] up to and including the final element a[n].

答案 1 :(得分:0)

以下是一种方法:

import java.util.List;
import java.util.ArrayList;

public class Main {


    public class LexicographicPermutations {

        private final String baseString;
        private final List<String> permutations = new ArrayList<String>();

        public LexicographicPermutations(String baseString) {
            this.baseString = baseString;
        }

        public List<String> getPermutations() {
            recursePermutations(baseString.length(), "");
            return permutations;
        }

        private void recursePermutations(int recursionsLeft, String result) {
            if(recursionsLeft == 0) {
                if(permutations.contains(result)) {
                    return;
                }
                int total = 0;
                StringBuilder resultTemp = new StringBuilder(result);
                for(int current = 0; current < baseString.length(); current ++) {
                    int position = resultTemp.indexOf(String.valueOf(baseString.charAt(current)));
                    if(position >= 0) {
                        resultTemp.deleteCharAt(position);
                    }
                }
                if (resultTemp.length() == 0) {
                    permutations.add(result);
                }
                return;
            }
            for(int index = 0; index < baseString.length(); index ++) {
                recursePermutations(recursionsLeft - 1, result + baseString.charAt(index));
            }
        }
    }

    public static void main(String[] args) {
        for(String permutation : new Main().new LexicographicPermutations("012").getPermutations()) {
            System.out.println(permutation);
        }
    }
}

答案 2 :(得分:0)

我的版本将字符串反汇编为charArray,对数组进行排序,对出现的字符进行计数并将它们写入Map中,然后使用for循环构建一个递归树,以避免重复。所以我的版本在O(c ^ n)中运行,即使一个字符多次出现并且保证是正确的。当角色出现不止一次时,Moishe Lipskers版本应该会慢一些,我认为nogards解决方案可能会产生重复,但我不确定。

@RunWith(AndroidJUnit4.class)
public class MyActivityTestTest {

    private MyObject myObj;

    @Rule
    // third parameter is set to false which means the activity is not started automatically
    public ActivityTestRule<MyActivity> mActivityRule =
        new ActivityTestRule<>(MyActivity.class, false, false);


    @Test
    public void testName() {

          myObj = MyObject.mockObject();
          Intent i = new Intent();
          i.putExtra("myobj", myObj);
          mActivityRule.launchActivity(i);

         //...
    }

}