Guest User

Untitled

a guest
Nov 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. public class PermutationSequence {
  2. /*
  3. dfs, backtracking
  4. Space complexity: O(1)
  5. time complexity in backtracking: factorial
  6. */
  7.  
  8. /*
  9. time limit exceeded
  10. when n=9, k=54494
  11. */
  12. public String getPermutation1(int n, int k) {
  13. int[] array = new int[n+1];
  14. int[] used = new int[n+1];
  15. for(int i=1; i<=n; i++){
  16. array[i] = i;
  17. }
  18. List<List<Integer>> ans = new LinkedList();
  19. backtracking1(array, new LinkedList<Integer>(), used, ans);
  20. List<Integer> temp = ans.get(k-1);
  21. StringBuilder sb = new StringBuilder();
  22. for(int i:temp){
  23. sb.append(i);
  24. }
  25. return sb.toString();
  26. }
  27.  
  28. private void backtracking1(int[] array, List<Integer> curr, int[] used, List<List<Integer>> ans){
  29. if(curr.size() == array.length-1){
  30. ans.add(new LinkedList<Integer>(curr));
  31. } else {
  32. for(int i=1; i<array.length; i++){
  33. if(used[i]!=1){
  34. curr.add(array[i]);
  35. used[i] = 1;
  36. backtracking1(array, curr, used, ans);
  37. used[i] = 0;
  38. curr.remove(curr.size()-1);
  39. }
  40. }
  41. }
  42. }
  43.  
  44. /*
  45. less space
  46. but still time limit exceed.
  47. */
  48. int index = 0;
  49. public String getPermutation2(int n, int k) {
  50. int[] array = new int[n+1];
  51. int[] used = new int[n+1];
  52. index = k;
  53. for(int i=1; i<=n; i++){
  54. array[i] = i;
  55. }
  56. StringBuilder sb = new StringBuilder();
  57. backtracking2(array, new LinkedList<Integer>(), used, sb);
  58. if(index>1)
  59. return "";
  60. return sb.toString();
  61. }
  62.  
  63. private void backtracking2(int[] array, List<Integer> curr, int[] used, StringBuilder sb){
  64. if(curr.size() == array.length-1){
  65. if(index==1){
  66. for(int i:curr){
  67. sb.append(i);
  68. }
  69. }
  70. index--;
  71. } else {
  72. for(int i=1; i<array.length; i++){
  73. if(used[i]!=1){
  74. curr.add(array[i]);
  75. used[i] = 1;
  76. backtracking2(array, curr, used, sb);
  77. used[i] = 0;
  78. curr.remove(curr.size()-1);
  79. }
  80. }
  81. }
  82. }
  83.  
  84. /*
  85. math way:
  86. 对于n个字符组成的字符串{1,2,3,...,n},取第k个数时,首先可以求出第一个数,即(k-1)/(n-1个数的排列个数)。
  87.  
  88. 比如n=3,k=4时,全排列组合为:
  89.  
  90. 1 + {2,3}的全排列
  91. 2 + {1,3}的全排列
  92. 3 + {1,2}的全排列
  93.  
  94. 可以一层一层的求出第k个顺序的字符串。
  95.  
  96. 时间复杂度为O(N)。
  97. */
  98.  
  99. /*
  100. recursive
  101. */
  102. public String getPermutation(int n, int k) {
  103. char[] nums = new char[]{'1','2','3','4','5','6','7','8','9'};
  104. String s = "";
  105. for(int i=0;i<n;i++) {
  106. s += nums[i];
  107. }
  108. return fun(new StringBuffer(s), k);
  109. }
  110.  
  111. public String fun(StringBuffer s, int k) {
  112. if(k<0 || s.toString().equals("")) return "";
  113. int cnt = 1, tmp = s.length()-1;
  114. //求阶乘,即当前位的循环周期
  115. while(tmp > 1) {
  116. cnt*=tmp;
  117. tmp-=1;
  118. }
  119. int pos = (k-1)/cnt;
  120. return s.charAt(pos) + fun(s.deleteCharAt(pos), k - pos*cnt);
  121. }
  122. }
Add Comment
Please, Sign In to add comment