给两个已序数组,写一个函数求出第K小的数(听说是GOOGLE给出的题)

给两个已序数组,写一个函数求出第K小的数(听说是GOOGLE给出的题)

根据上面题 目,随便写了一个JAVA版的实现。

package com.test;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a1[] = { 5, 6, 7, 8, 48, 51, 55, 56, 182 };
int a2[] = { 4, 4, 5, 9, 41, 155, 522, 1566 };
Test.getNumber(1, a1, a2);
}

public static String getNumber(int index, int[] a1, int[] a2) {
LinkList list = null;
LinkList listTmp = null;
LinkList tmp = null;
int indexTmp = 0;
int length1 = a1.length, length2 = a2.length;
int p1 = 0, p2 = 0;
/*
* 验证输入index是否在合法的范围内
*/
if (length1 + length2 < index) {
System.out
.println("k超过了两个数据的长度!![如:a1长度为5,a2长度为12,index则取值为:1-17]");
return null;
}
/*
* 开始构造LinkList链表,将已排序的两个数组的数据封装到一个链表里边,并添加附加信息,如链表的索引index,相应数据在原来两个数组中的哪个里边local
*/
while (p1 < length1 && p2 < length2) {

if (null == list) {
list = new LinkList();
list.setLocal((byte) 0);
list.setIndex(indexTmp);
indexTmp++;
listTmp = list;
}
if (a1[p1] <= a2[p2]) {
tmp = new LinkList();
tmp.setLocal((byte) 1);
tmp.setValue(a1[p1]);
tmp.setListNode(null);
tmp.setIndex(indexTmp);
indexTmp++;
listTmp.setListNode(tmp);
listTmp = tmp;
p1++;
} else {
tmp = new LinkList();
tmp.setLocal((byte) 2);
tmp.setValue(a2[p2]);
tmp.setListNode(null);
tmp.setIndex(indexTmp);
indexTmp++;
listTmp.setListNode(tmp);
listTmp = tmp;
p2++;
}

}
/*
* 下面两个if只执行其中一个,视两个数据a1,a2的长度,以及数据来决定
*/
if (p1 < length1) {
for (; p1 < length1; p1++) {
tmp = new LinkList();
tmp.setLocal((byte) 1);
tmp.setValue(a1[p1]);
tmp.setListNode(null);
tmp.setIndex(indexTmp);
indexTmp++;
listTmp.setListNode(tmp);
listTmp = tmp;
}
}

if (p2 < length2) {
for (; p2 < length2; p2++) {
tmp = new LinkList();
tmp.setLocal((byte) 2);
tmp.setValue(a2[p2]);
tmp.setListNode(null);
tmp.setIndex(indexTmp);
indexTmp++;
listTmp.setListNode(tmp);
listTmp = tmp;

}


/*
* 找到最小第K个数据,并返回
*/
while ((list.getListNode()) != null) {
if (list.getIndex() == index) {
String message = "第" + list.getIndex() + "个最小的数为"
+ list.getValue() + " 它在第" + list.getLocal()
+ "个数组里 ";
System.out.println(message);
return list.getValue() + "";
}

list = list.getListNode();
}

if (list.getIndex() == index) {
String message = "第" + list.getIndex() + "个最小的数为"
+ list.getValue() + " 它在第" + list.getLocal()
+ "个数组里 ";
System.out.println(message);
return list.getValue() + "";
}


return null;
}
}

class LinkList {
private int index;//链表,用于存放丙个数组里数据
private int value;
private byte local;// 0:表示链表头;1:数组1;2:数组2
private LinkList listNode;//指向下一个节点

public LinkList() {
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public byte getLocal() {
return local;
}

public void setLocal(byte local) {
this.local = local;
}

public LinkList getListNode() {
return listNode;
}

public void setListNode(LinkList listNode) {
this.listNode = listNode;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值