Java面試題集(136-150)
阿新 • • 發佈:2018-11-10
分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow
Java程式設計師面試題集(136-150)
摘要:這一部分主要是資料結構和演算法相關的面試題目,雖然只有15道題目,但是包含的資訊量還是很大的,很多題目背後的解題思路和演算法是非常值得玩味的。
136、給出下面的二叉樹先序、中序、後序遍歷的序列?
答:先序序列:ABDEGHCF;中序序列:DBGEHACF;後序序列:DGHEBFCA。
補充:二叉樹也稱為二分樹,它是樹形結構的一種,其特點是每個結點至多有二棵子樹,並且二叉樹的子樹有左右之分,其次序不能任意顛倒。二叉樹的遍歷序列按照訪問根節點的順序分為先序(先訪問根節點,接下來先序訪問左子樹,再先序訪問右子樹)、中序(先中序訪問左子樹,然後訪問根節點,最後中序訪問右子樹)和後序(先後序訪問左子樹,再後序訪問右子樹,最後訪問根節點)。如果知道一棵二叉樹的先序和中序序列或者中序和後序序列,那麼也可以還原出該二叉樹。
例如,已知二叉樹的先序序列為:xefdzmhqsk,中序序列為:fezdmxqhks,那麼還原出該二叉樹應該如下圖所示:
137、你知道的排序演算法都哪些?用Java寫一個排序系統。
答:穩定的排序演算法有:插入排序、選擇排序、氣泡排序、雞尾酒排序、歸併排序、二叉樹排序、基數排序等;不穩定排序演算法包括:希爾排序、堆排序、快速排序等。
下面是關於排序演算法的一個列表:
下面按照策略模式給出一個排序系統,實現了冒泡、歸併和快速排序。
Sorter.java
-
package
com.jackfrued.util;
-
-
import java.util.Comparator;
-
-
/**
-
* 排序器介面(策略模式: 將演算法封裝到具有共同介面的獨立的類中使得它們可以相互替換)
-
* @author駱昊
-
*
-
*/
-
public
interface Sorter {
-
-
/**
-
* 排序
-
* @param list 待排序的陣列
-
*/
-
public <T extends Comparable<T>>
void sort(T[] list);
-
-
/**
-
* 排序
-
* @param list 待排序的陣列
-
* @param comp 比較兩個物件的比較器
-
*/
-
public <T>
void sort(T[] list, Comparator<T> comp);
-
}
BubbleSorter.java
-
package com.jackfrued.util;
-
-
import java.util.Comparator;
-
-
/**
-
* 氣泡排序
-
* @author駱昊
-
*
-
*/
-
public
class BubbleSorter implements Sorter {
-
-
@Override
-
public <T extends Comparable<T>>
void sort(T[] list) {
-
boolean swapped =
true;
-
for(
int i =
1; i < list.length && swapped;i++) {
-
swapped=
false;
-
for(
int j =
0; j < list.length - i; j++) {
-
if(list[j].compareTo(list[j+
1]) >
0 ) {
-
T temp = list[j];
-
list[j]= list[j +
1];
-
list[j+
1] = temp;
-
swapped=
true;
-
}
-
}
-
}
-
}
-
-
@Override
-
public <T>
void sort(T[] list,Comparator<T> comp) {
-
boolean swapped =
true;
-
for(
int i =
1; i < list.length && swapped; i++) {
-
swapped =
false;
-
for(
int j =
0; j < list.length - i; j++) {
-
if(comp.compare(list[j], list[j +
1]) >
0 ) {
-
T temp = list[j];
-
list[j]= list[j +
1];
-
list[j+
1] = temp;
-
swapped=
true;
-
}
-
}
-
}
-
}
-
}
MergeSorter.java
-
package com.jackfrued.util;
-
-
import java.util.Comparator;
-
-
/**
-
* 歸併排序
-
* 歸併排序是建立在歸併操作上的一種有效的排序演算法。
-
* 該演算法是採用分治法(divide-and-conquer)的一個非常典型的應用,
-
* 先將待排序的序列劃分成一個一個的元素,再進行兩兩歸併,
-
* 在歸併的過程中保持歸併之後的序列仍然有序。
-
* @author駱昊
-
*
-
*/
-
public
class MergeSorter implements Sorter {
-
-
@Override
-
public <T extends Comparable<T>>
void sort(T[] list) {
-
T[] temp = (T[])
new Comparable[list.length];
-
mSort(list,temp,
0, list.length-
1);
-
}
-
-
private <T extends Comparable<T>>
void mSort(T[] list, T[] temp, int low, int high) {
-
if(low == high) {
-
return ;
-
}
-
else {
-
int mid = low + ((high -low) >>
1);
-
mSort(list,temp, low, mid);
-
mSort(list,temp, mid +
1, high);
-
merge(list,temp, low, mid +
1, high);
-
}
-
}
-
-
private <T extends Comparable<T>>
void merge(T[] list, T[] temp, int left, int right, int last) {
-
int j =
0;
-
int lowIndex = left;
-
int mid = right -
1;
-
int n = last - lowIndex +
1;
-
while (left <= mid && right <= last){
-
if (list[left].compareTo(list[right]) <
0){
-
temp[j++] = list[left++];
-
}
else {
-
temp[j++] = list[right++];
-
}
-
}
-
while (left <= mid) {
-
temp[j++] = list[left++];
-
}
-
while (right <= last) {
-
temp[j++] = list[right++];
-
}
-
for (j =
0; j < n; j++) {
-
list[lowIndex + j] = temp[j];
-
}
-
}
-
-
@Override
-
public <T>
void sort(T[] list, Comparator<T> comp) {
-
T[]temp = (T[])
new Comparable[list.length];
-
mSort(list,temp,
0, list.length-
1, comp);
-
}
-
-
private <T>
void mSort(T[] list, T[] temp, int low, int high, Comparator<T> comp) {
-
if(low == high) {
-
return ;
-
}
-
else {
-
int mid = low + ((high -low) >>
1);
-
mSort(list,temp, low, mid, comp);
-
mSort(list,temp, mid +
1, high, comp);
-
merge(list,temp, low, mid +
1, high, comp);
-
}
-
}
-
-
private <T>
void merge(T[] list, T[]temp, int left, int right, int last, Comparator<T> comp) {
-
int j =
0;
-
int lowIndex = left;
-
int mid = right -
1;
-
int n = last - lowIndex +
1;
-
while (left <= mid && right <= last){
-
if (comp.compare(list[left], list[right]) <
0) {
-
temp[j++] = list[left++];
-
}
else {
-
temp[j++] = list[right++];
-
}
-
}
-
while (left <= mid) {
-
temp[j++] = list[left++];
-
}
-
while (right <= last) {
-
temp[j++] = list[right++];
-
}
-
for (j =
0; j < n; j++) {
-
list[lowIndex + j] = temp[j];
-
}
-
}
-
-
}
QuickSorter.java
-
package com.jackfrued.util;
-
-
import java.util.Comparator;
-
-
/**
-
* 快速排序
-
* 快速排序是使用分治法(divide-and-conquer)依選定的樞軸
-
* 將待排序序列劃分成兩個子序列,其中一個子序列的元素都小於樞軸,
-
* 另一個子序列的元素都大於或等於樞軸,然後對子序列重複上面的方法,
-
* 直到子序列中只有一個元素為止
-
* @author Hao
-
*
-
*/
-
public
class QuickSorter implements Sorter {
-
-
@Override
-
public <T extends Comparable<T>>
void sort(T[] list) {
-
quickSort(list,
0, list.length-
1);
-
}
-
-
@Override
-
public <T>
void sort(T[] list, Comparator<T> comp) {
-
quickSort(list,
0, list.length-
1, comp);
-
}
-
-
private <T extends Comparable<T>>
void quickSort(T[] list, int first, int last) {
-
if (last > first) {
-
int pivotIndex = partition(list, first, last);
-
quickSort(list, first, pivotIndex -
1);
-
quickSort(list, pivotIndex, last);
-
}
-
}
-
-
private <T>
void quickSort(T[] list, int first, int last,Comparator<T> comp) {
-
if (last > first) {
-
int pivotIndex = partition(list, first, last, comp);
-
quickSort(list, first, pivotIndex -
1, comp);
-
quickSort(list, pivotIndex, last, comp);
-
}
-
}
-
-
private <T extends Comparable<T>>
int partition(T[] list, int first, int last) {
-
T pivot = list[first];
-
int low = first +
1;
-
int high = last;
-
-
while (high > low) {
-
while (low <= high && list[low].compareTo(pivot) <=
0) {
-
low++;
-
}
-
while (low <= high && list[high].compareTo(pivot) >=
0) {
-
high--;
-
}
-
if (high > low) {
-
T temp = list[high];
-
list[high]= list[low];
-
list[low]= temp;
-
}
-
}
-
-
while (high > first&& list[high].compareTo(pivot) >=
0) {
-
high--;
-
}
-
if (pivot.compareTo(list[high])>
0) {
-
list[first]= list[high]