package com.dengpf.interview;
/**
* Created by kobe73er on 16/11/11.
*/
public class ClientInterview {
public static int[] getSortedArray(int a[], int b[]) {
int result[] = new int[a.length + b.length];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
result[k++] = a[i++];
} else {
result[k++] = b[j++];
}
}
while (i < a.length) {
result[k++] = a[i++];
}
while (j < b.length) {
result[k++] = b[j++];
}
return result;
}
public static void main(String args[]) {
int a[] = {1, 2, 3, 4, 5, 5, 5};
int b[] = {6, 7, 8, 9, 10, 34, 56};
int[] result = getSortedArray(a, b);
for (int index = 0; index < result.length; index++) {
System.out.println(result[index]);
}
}
}
两个有序数组合并为一个有序数组
最新推荐文章于 2022-05-17 20:39:20 发布