一个口试的排序题
一个面试的排序题
坛子里一个哥们的面试题:
数组A与数组B均已排好序,用最有效率的办法将其合并成为数组C,要考虑到重复的数字将其去重
public class test { public static int[] b = { 1, 3, 5, 7, 9, 10, 11, 90 }; public static int[] a = { 2, 5, 7, 8, 9 }; public static void main(String[] args) { test1(a,b); Map<Integer,Integer> map=sortByTreeMap(a,b); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry)iterator.next(); System.out.print(mapentry.getValue()+" "); } } public static void test1(int[] a, int[] b){ int[] c = 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]) { if (a[i] == b[j]) { j++; } else { c[k] = a[i]; i++; k++; } } else { c[k] = b[j]; j++; k++; } } while (i < a.length) { c[k] = a[i]; k++; i++; } while (j < b.length) { c[k] = b[j]; j++; k++; } System.out.println(Arrays.toString(c)); } public static Map sortByTreeMap(int[] a, int[] b){ Map<Integer,Integer> map=new TreeMap<Integer,Integer>(); for(int i=0;i<a.length;i++){ map.put(a[i], a[i]); } for(int i=0;i<b.length;i++){ map.put(b[i], b[i]); } return map; } }
1 楼
qingralf
2011-04-11
$b = array(1, 3, 5, 7, 9, 10, 11, 90 ); $a = array( 2, 5, 7, 8, 9); print_r($a+$b);