首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

归拢顺序数组

2013-01-26 
合并顺序数组public class MergeArray {public static int[] doMerge(int[] a, int[] b) {int[] tmp new

合并顺序数组
public class MergeArray {public static int[] doMerge(int[] a, int[] b) {int[] tmp = new int[a.length + b.length];tmp[0] = 0;int i = 0, j = 0, k = 0, count = 0;while (i <= a.length - 1 && j <= b.length - 1) {if (a[i] < b[j]) {if (a[i] != tmp[k]) {tmp[k++] = a[i++];count++;} else {i++;}} else if (a[i] > b[j]) {if (b[j] != tmp[k]) {tmp[k++] = b[j++];count++;} else {j++;}} else {if (a[i] != tmp[k]) {tmp[k++] = a[i++];count++;} else {i++;}j++;}}while (i <= a.length - 1) {if (a[i] != tmp[k]) {tmp[k++] = a[i++];count++;} else {i++;}}while (j <= b.length - 1) {if (b[j] != tmp[k]) {tmp[k++] = b[j++];count++;} else {j++;}}int[] c = new int[count];for (int m = 0; m <= count - 1 ; m++) {c[m] = tmp[m];}return c;}public static void main(String[] args) {int[] a = { 1, 2, 4, 8 };int[] b = { 1, 3, 5, 8 };int[] c = MergeArray.doMerge(a, b);for (int i = 0; i <= c.length - 1; i++) {System.out.print(c[i] + " ");}}}?

热点排行