六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 67|回复: 0

一个面试的排序题

[复制链接]

升级  40%

92

主题

92

主题

92

主题

举人

Rank: 3Rank: 3

积分
320
 楼主| 发表于 2013-1-19 04:11:41 | 显示全部楼层 |阅读模式
坛子里一个哥们的面试题:
         数组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 <= b[j]) {if (a == b[j]) {j++;} else {c[k] = a;i++;k++;}} else {c[k] = b[j];j++;k++;}}while (i < a.length) {c[k] = a;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, a);        }        for(int i=0;i<b.length;i++){            map.put(b, b);        }        return map;}}  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表