您的位置:首页 > 其它

有序数组合并

2015-07-14 12:27 267 查看
package com.hhx;

/**
* Created by hyson on 15/7/14.
*/
public class CombinArray {
public static void main(String[] args) {
int[] a = {-1, -3, 2, 4};
int[] b = {3, 5, 6};
int[] result = combineArray(a, b);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}

public static int[] combineArray(int[] a, int[] b) {
int[] result = new int[a.length + b.length];

int i = 0, j = 0, 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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: