1. 程式人生 > >Java面試題集(136-150)

Java面試題集(136-150)

分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow

Java程式設計師面試題集(136-150)

摘要:這一部分主要是資料結構和演算法相關的面試題目,雖然只有15道題目,但是包含的資訊量還是很大的,很多題目背後的解題思路和演算法是非常值得玩味的。

136、給出下面的二叉樹先序、中序、後序遍歷的序列?


答:先序序列:ABDEGHCF;中序序列:DBGEHACF;後序序列:DGHEBFCA。

補充:二叉樹也稱為二分樹,它是樹形結構的一種,其特點是每個結點至多有二棵子樹,並且二叉樹的子樹有左右之分,其次序不能任意顛倒。二叉樹的遍歷序列按照訪問根節點的順序分為先序(先訪問根節點,接下來先序訪問左子樹,再先序訪問右子樹)、中序(先中序訪問左子樹,然後訪問根節點,最後中序訪問右子樹)和後序(先後序訪問左子樹,再後序訪問右子樹,最後訪問根節點)。如果知道一棵二叉樹的先序和中序序列或者中序和後序序列,那麼也可以還原出該二叉樹。

例如,已知二叉樹的先序序列為:xefdzmhqsk,中序序列為:fezdmxqhks,那麼還原出該二叉樹應該如下圖所示:

 

137、你知道的排序演算法都哪些?用Java寫一個排序系統。

答:穩定的排序演算法有:插入排序、選擇排序、氣泡排序、雞尾酒排序、歸併排序、二叉樹排序、基數排序等;不穩定排序演算法包括:希爾排序、堆排序、快速排序等。

下面是關於排序演算法的一個列表:


下面按照策略模式給出一個排序系統,實現了冒泡、歸併和快速排序。

Sorter.java


  
  1. package
    com.jackfrued.util;
  2. import java.util.Comparator;
  3. /**
  4. * 排序器介面(策略模式: 將演算法封裝到具有共同介面的獨立的類中使得它們可以相互替換)
  5. * @author駱昊
  6. *
  7. */
  8. public interface Sorter {
  9. /**
  10. * 排序
  11. * @param list 待排序的陣列
  12. */
  13. public <T extends Comparable<T>> void sort(T[] list);
  14. /**
  15. * 排序
  16. * @param list 待排序的陣列
  17. * @param comp 比較兩個物件的比較器
  18. */
  19. public <T> void sort(T[] list, Comparator<T> comp);
  20. }

 

BubbleSorter.java


  
  1. package com.jackfrued.util;
  2. import java.util.Comparator;
  3. /**
  4. * 氣泡排序
  5. * @author駱昊
  6. *
  7. */
  8. public class BubbleSorter implements Sorter {
  9. @Override
  10. public <T extends Comparable<T>> void sort(T[] list) {
  11. boolean swapped = true;
  12. for( int i = 1; i < list.length && swapped;i++) {
  13. swapped= false;
  14. for( int j = 0; j < list.length - i; j++) {
  15. if(list[j].compareTo(list[j+ 1]) > 0 ) {
  16. T temp = list[j];
  17. list[j]= list[j + 1];
  18. list[j+ 1] = temp;
  19. swapped= true;
  20. }
  21. }
  22. }
  23. }
  24. @Override
  25. public <T> void sort(T[] list,Comparator<T> comp) {
  26. boolean swapped = true;
  27. for( int i = 1; i < list.length && swapped; i++) {
  28. swapped = false;
  29. for( int j = 0; j < list.length - i; j++) {
  30. if(comp.compare(list[j], list[j + 1]) > 0 ) {
  31. T temp = list[j];
  32. list[j]= list[j + 1];
  33. list[j+ 1] = temp;
  34. swapped= true;
  35. }
  36. }
  37. }
  38. }
  39. }

 

MergeSorter.java


  
  1. package com.jackfrued.util;
  2. import java.util.Comparator;
  3. /**
  4. * 歸併排序
  5. * 歸併排序是建立在歸併操作上的一種有效的排序演算法。
  6. * 該演算法是採用分治法(divide-and-conquer)的一個非常典型的應用,
  7. * 先將待排序的序列劃分成一個一個的元素,再進行兩兩歸併,
  8. * 在歸併的過程中保持歸併之後的序列仍然有序。
  9. * @author駱昊
  10. *
  11. */
  12. public class MergeSorter implements Sorter {
  13. @Override
  14. public <T extends Comparable<T>> void sort(T[] list) {
  15. T[] temp = (T[]) new Comparable[list.length];
  16. mSort(list,temp, 0, list.length- 1);
  17. }
  18. private <T extends Comparable<T>> void mSort(T[] list, T[] temp, int low, int high) {
  19. if(low == high) {
  20. return ;
  21. }
  22. else {
  23. int mid = low + ((high -low) >> 1);
  24. mSort(list,temp, low, mid);
  25. mSort(list,temp, mid + 1, high);
  26. merge(list,temp, low, mid + 1, high);
  27. }
  28. }
  29. private <T extends Comparable<T>> void merge(T[] list, T[] temp, int left, int right, int last) {
  30. int j = 0;
  31. int lowIndex = left;
  32. int mid = right - 1;
  33. int n = last - lowIndex + 1;
  34. while (left <= mid && right <= last){
  35. if (list[left].compareTo(list[right]) < 0){
  36. temp[j++] = list[left++];
  37. } else {
  38. temp[j++] = list[right++];
  39. }
  40. }
  41. while (left <= mid) {
  42. temp[j++] = list[left++];
  43. }
  44. while (right <= last) {
  45. temp[j++] = list[right++];
  46. }
  47. for (j = 0; j < n; j++) {
  48. list[lowIndex + j] = temp[j];
  49. }
  50. }
  51. @Override
  52. public <T> void sort(T[] list, Comparator<T> comp) {
  53. T[]temp = (T[]) new Comparable[list.length];
  54. mSort(list,temp, 0, list.length- 1, comp);
  55. }
  56. private <T> void mSort(T[] list, T[] temp, int low, int high, Comparator<T> comp) {
  57. if(low == high) {
  58. return ;
  59. }
  60. else {
  61. int mid = low + ((high -low) >> 1);
  62. mSort(list,temp, low, mid, comp);
  63. mSort(list,temp, mid + 1, high, comp);
  64. merge(list,temp, low, mid + 1, high, comp);
  65. }
  66. }
  67. private <T> void merge(T[] list, T[]temp, int left, int right, int last, Comparator<T> comp) {
  68. int j = 0;
  69. int lowIndex = left;
  70. int mid = right - 1;
  71. int n = last - lowIndex + 1;
  72. while (left <= mid && right <= last){
  73. if (comp.compare(list[left], list[right]) < 0) {
  74. temp[j++] = list[left++];
  75. } else {
  76. temp[j++] = list[right++];
  77. }
  78. }
  79. while (left <= mid) {
  80. temp[j++] = list[left++];
  81. }
  82. while (right <= last) {
  83. temp[j++] = list[right++];
  84. }
  85. for (j = 0; j < n; j++) {
  86. list[lowIndex + j] = temp[j];
  87. }
  88. }
  89. }

 

QuickSorter.java


  
  1. package com.jackfrued.util;
  2. import java.util.Comparator;
  3. /**
  4. * 快速排序
  5. * 快速排序是使用分治法(divide-and-conquer)依選定的樞軸
  6. * 將待排序序列劃分成兩個子序列,其中一個子序列的元素都小於樞軸,
  7. * 另一個子序列的元素都大於或等於樞軸,然後對子序列重複上面的方法,
  8. * 直到子序列中只有一個元素為止
  9. * @author Hao
  10. *
  11. */
  12. public class QuickSorter implements Sorter {
  13. @Override
  14. public <T extends Comparable<T>> void sort(T[] list) {
  15. quickSort(list, 0, list.length- 1);
  16. }
  17. @Override
  18. public <T> void sort(T[] list, Comparator<T> comp) {
  19. quickSort(list, 0, list.length- 1, comp);
  20. }
  21. private <T extends Comparable<T>> void quickSort(T[] list, int first, int last) {
  22. if (last > first) {
  23. int pivotIndex = partition(list, first, last);
  24. quickSort(list, first, pivotIndex - 1);
  25. quickSort(list, pivotIndex, last);
  26. }
  27. }
  28. private <T> void quickSort(T[] list, int first, int last,Comparator<T> comp) {
  29. if (last > first) {
  30. int pivotIndex = partition(list, first, last, comp);
  31. quickSort(list, first, pivotIndex - 1, comp);
  32. quickSort(list, pivotIndex, last, comp);
  33. }
  34. }
  35. private <T extends Comparable<T>> int partition(T[] list, int first, int last) {
  36. T pivot = list[first];
  37. int low = first + 1;
  38. int high = last;
  39. while (high > low) {
  40. while (low <= high && list[low].compareTo(pivot) <= 0) {
  41. low++;
  42. }
  43. while (low <= high && list[high].compareTo(pivot) >= 0) {
  44. high--;
  45. }
  46. if (high > low) {
  47. T temp = list[high];
  48. list[high]= list[low];
  49. list[low]= temp;
  50. }
  51. }
  52. while (high > first&& list[high].compareTo(pivot) >= 0) {
  53. high--;
  54. }
  55. if (pivot.compareTo(list[high])> 0) {
  56. list[first]= list[high]