百度笔试题,不会做啊。请大家做做

li1gang11 2007-09-10 01:27:13
加精
1 完成函数
size_t foo(unsigned int *a1, size_t al1, unsigned int* a2, size_t al2)
其中a1和a2都为无符号数组,al1和al2为数组的长度,数组的长度为偶数。
无符号数组由一对数字区间组成。如下例:
a1 为 0,1,3,6,10,20
a2 为 0,1,20,50,4,5
则 a1表示以下区间[0,1] [3,6] [10,20]
a2表示以下区间[0,1] [20,50] [4,5]
则a1,a2的重叠部分为[0,1] [4,5],其长度为2
函数foo要求返回重叠区间的长度。上例中为2.

要求:
详细说明自己的解题思路,说明自己实现的一些关键点。
写出函数foo原代码,另外效率尽量高,并给出代码的复杂性分析。
限制:
al1和al2的长度不超过100万。而且同一个数组的区间可能出现重重叠。
如a1可能为 0,5, 4,8, 9,100, 70,80
使用的存储空间尽量小。


2 多人排成一个队列,我们认为从低到高是正确的序列,但是总有部分人不遵守秩序。如果说,前面的人比后面的人高(两人身高一样认为是合适的),那么我们就认为这两个人是一对“捣乱分子”,比如说,现在存在一个序列:
176, 178, 180, 170, 171
这些捣乱分子对为<176, 170>, <176, 171>, <178, 170>, <178, 171>, <180, 170>, <180, 171>,
那么,现在给出一个整型序列,请找出这些捣乱分子对的个数(仅给出捣乱分子对的数目即可,不用具体的对)

要求:
输入:
为一个文件(in),文件的每一行为一个序列。序列全为数字,数字间用”,”分隔。
输出:
为一个文件(out),每行为一个数字,表示捣乱分子的对数。

详细说明自己的解题思路,说明自己实现的一些关键点。并给出实现的代码 ,并分析时间复杂度。

限制:
输入每行的最大数字个数为100000个,数字最长为6位。程序无内存使用限制。


二、下面是两道选做题,请根据自己的情况选择其中的一道作答(WEB方向请答第4道,其他职位方向答第3道)。

3
考虑一个在线好友系统。系统为每个用户维护一个好友列表,列表限制最多可以有500个好友,好友必须是这个系统中的其它用户。好友关系是单向的,用户B是用户A的好友,但A不一定是B的好友。

用户以ID形式表示,现给出好友列表数据的文本形式如下:
1 3,5,7,67,78,3332
2 567,890
31 1,66
14 567
78 10000

每行数据有两列,第一列为用户ID,第二列为其好友ID,不同ID间用”,”分隔,ID升序排列。列之间用”t”分隔。


要求:
请设计合适的索引数据结构,来完成以下查询:
给定用户A和B,查询A和B之间是否有这样的关系:B是A的二维好友(好友的好友)。
如上例中,10000为1的二维好友,因为78为1的好友,10000为78的好友。

详细说明自己的解题思路,说明自己实现的一些关键点。并给出实现的伪代码实现建立索引过程和查询过程,并说明空间和时间复杂度。

限制:
用户数量不超过1000万,平均50个好友。

4
有关系模式:User(userId, userName), Article(articleId, userId, title, content),Vote(articleId, score),User为用户关系,Article为用户发表的文章关系,Vote为文章得票关系,title为文章标题、score为得票数。
(1)用SQL语言查询所有没发表过文章的用户名;
(2)用SQL语言查询得票数大于100的所有文章标题,按得票数倒序排列;
(3)用SQL语言查询出发表文章数大于5,文章平均得票数大于100的用户名,按平均得票数倒序排列;
(4)设计这些表的主键、外键和索引,并指出上面三个查询所使用的索引。
(5)当用户数超过1000万,文章数超过1亿时,如何考虑存储及性能的改进和优化?
...全文
9870 137 打赏 收藏 转发到动态 举报
写回复
用AI写文章
137 条回复
切换为时间正序
请发表友善的回复…
发表回复
tshichun 2012-10-16
  • 打赏
  • 举报
回复
不错,顶起,标记!
s3178 2012-10-11
  • 打赏
  • 举报
回复
。。。。。。。我是无语了。什么也不想了。
ljq550000 2011-09-03
  • 打赏
  • 举报
回复
mark
wwai168 2011-08-28
  • 打赏
  • 举报
回复
mark
phoenix_share 2011-08-18
  • 打赏
  • 举报
回复
public class Chapter {
int[] a={0,2,5,6,9,19,3,4,8,13};
int[] b={2,4,20,25,23,24,1,6};

public int foo(int[] a,int al,int[] b,int bl){
LinkList lista=new LinkList(); //定义一个链表存放数组a的并集
for(int i=0;i<a.length;i+=2){
Node node=new Node(a[i],a[i+1]);
lista.union(node); //对数组a求并集
}
LinkList listb=new LinkList(); //定义一个链表存放数组b的并集
for(int i=0;i<b.length;i+=2){
Node node=new Node(b[i],b[i+1]);
listb.union(node); //对数组b求并集
}
LinkList listc=lista.intersection(listb); //定义一个链表存放a\b的 交集
Node first=listc.getNode().getNext();
int sum=0;
while(first!=null){ //遍历a\b的交集得到结果
sum+=first.getEnd()-first.getStart();
System.out.println(first.getStart()+" "+first.getEnd());
first=first.getNext();
}
return sum;
}
public static void main(String[] args) {
Chapter chan=new Chapter();
System.out.println("相交的长度为:"+
chan.foo(chan.a,chan.a.length,chan.b,chan.b.length));
}
}

class Node{
private int start;
private int end;
private Node next;
Node(){
start=-1;
end=-1;
next=null;
}
Node(int start,int end){
this.start=start;
this.end=end;
next=null;
}
public void setStart(int start){
this.start=start;
}
public void setEnd(int end){
this.end=end;
}
public void setNext(Node next){
this.next=next;
}
public int getStart(){
return start;
}
public int getEnd(){
return end;
}
public Node getNext(){
return next;
}
}

class LinkList{
private Node head;
LinkList(){
length=0;
head=new Node();
}
public Node getNode(){
return head;
}
public void union(Node node){
if(head.getNext()==null){
head.setNext(node);
return;
}else{
Node first=head.getNext();
Node rear=head;
while(first!=null){
if(node.getStart()>first.getEnd()){
if(first.getNext()==null){
first.setNext(node);
first=first.getNext();
rear=rear.getNext();
break;
}else{
first=first.getNext();
rear=rear.getNext();
}
}else if(node.getEnd()<first.getStart()){
node.setNext(first);
rear.setNext(node);
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()<first.getStart()&&
node.getEnd()<=first.getEnd()){
first.setStart(node.getStart());
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()<first.getStart()&&
node.getEnd()>first.getEnd()){
first.setStart(node.getStart());
first.setEnd(node.getEnd());
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()>first.getStart()&&
node.getEnd()>first.getEnd()){
first.setEnd(node.getEnd());
first=first.getNext();
rear=rear.getNext();
break;
}else{
first=first.getNext();
rear=rear.getNext();
break;
}
}
while(first!=null){
if(rear.getEnd()<first.getStart()){
return;
}else if(rear.getEnd()>=first.getStart()&&
rear.getEnd()<=first.getEnd()){
rear.setEnd(first.getEnd());
rear.setNext(first.getNext());
first=first.getNext();
}else if(rear.getEnd()>first.getEnd()){
rear.setNext(first.getNext());
first=first.getNext();
}
}

}
}

public LinkList intersection(LinkList lis){
LinkList list=new LinkList();
if(head.getNext()==null||lis.getNode().getNext()==null){
head.setNext(null);
return list;
}else{
Node firsta;
Node reara;
Node firstb=lis.getNode().getNext();
Node rearb=lis.getNode();
while(firstb!=null){
firsta=head.getNext();
reara=head;
while(firsta!=null){
if(firstb.getEnd()<=firsta.getStart()){
break;
}else if(firstb.getStart()<=firsta.getStart()&&
firstb.getEnd()<=firsta.getEnd()){
list.union(new Node(firsta.getStart(),firstb.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()<=firsta.getStart()&&
firstb.getEnd()>firsta.getEnd()){
list.union(new Node(firsta.getStart(),firsta.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()>firsta.getStart()&&
firstb.getEnd()<firsta.getEnd()){
list.union(new Node(firstb.getStart(),firstb.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()>firsta.getStart()&&
firstb.getStart()<firsta.getEnd()&&
firstb.getEnd()>firsta.getEnd()){
list.union(new Node(firstb.getStart(),firsta.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else{
reara=firsta;
firsta=firsta.getNext();
}
}
rearb=firstb;
firstb=firstb.getNext();
}
}
return list;
}
}
phoenix_share 2011-08-18
  • 打赏
  • 举报
回复
第一题主要是先求并集,然后求交集,可以用链表来做,可以动态的删除和添加,这样空间复制度应该最小,时间复杂度的话,求并集是O(n),求交集是O(n*n).
大家讨论了好多方法,但是一定要把题目弄明白,比如int[] a={0,1,6,12,11,12,3,4} int[] b={1,2,4,6,2,3}那么这两个数组相交的长度就是0,所以可以排除用位图法求解了.
我看前面贴了不少代码,但是得不到正确的结果,现在我给个java代码,大家讨论一下,希望找到最快最好的方法.

public class Chapter {
int[] a={0,2,5,6,9,19,3,4,8,13};
int[] b={2,4,20,25,23,24,1,6};

public int foo(int[] a,int al,int[] b,int bl){
LinkList lista=new LinkList(); //定义一个链表存放数组a的并集
for(int i=0;i<a.length;i+=2){
Node node=new Node(a[i],a[i+1]);
lista.union(node); //对数组a求并集
}
LinkList listb=new LinkList(); //定义一个链表存放数组b的并集
for(int i=0;i<b.length;i+=2){
Node node=new Node(b[i],b[i+1]);
listb.union(node); //对数组b求并集
}
LinkList listc=lista.intersection(listb); //定义一个链表存放a\b的 交集
Node first=listc.getNode().getNext();
int sum=0;
while(first!=null){ //遍历a\b的交集得到结果
sum+=first.getEnd()-first.getStart();
System.out.println(first.getStart()+" "+first.getEnd());
first=first.getNext();
}
return sum;
}
public static void main(String[] args) {
Chapter chan=new Chapter();
System.out.println("相交的长度为:"+
chan.foo(chan.a,chan.a.length,chan.b,chan.b.length));
}
}

class Node{
private int start;
private int end;
private Node next;
Node(){
start=-1;
end=-1;
next=null;
}
Node(int start,int end){
this.start=start;
this.end=end;
next=null;
}
public void setStart(int start){
this.start=start;
}
public void setEnd(int end){
this.end=end;
}
public void setNext(Node next){
this.next=next;
}
public int getStart(){
return start;
}
public int getEnd(){
return end;
}
public Node getNext(){
return next;
}
}

class LinkList{
private Node head;
LinkList(){
length=0;
head=new Node();
}
public Node getNode(){
return head;
}
public void union(Node node){
if(head.getNext()==null){
head.setNext(node);
return;
}else{
Node first=head.getNext();
Node rear=head;
while(first!=null){
if(node.getStart()>first.getEnd()){
if(first.getNext()==null){
first.setNext(node);
first=first.getNext();
rear=rear.getNext();
break;
}else{
first=first.getNext();
rear=rear.getNext();
}
}else if(node.getEnd()<first.getStart()){
node.setNext(first);
rear.setNext(node);
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()<first.getStart()&&
node.getEnd()<=first.getEnd()){
first.setStart(node.getStart());
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()<first.getStart()&&
node.getEnd()>first.getEnd()){
first.setStart(node.getStart());
first.setEnd(node.getEnd());
first=first.getNext();
rear=rear.getNext();
break;
}else if(node.getStart()>first.getStart()&&
node.getEnd()>first.getEnd()){
first.setEnd(node.getEnd());
first=first.getNext();
rear=rear.getNext();
break;
}else{
first=first.getNext();
rear=rear.getNext();
break;
}
}
while(first!=null){
if(rear.getEnd()<first.getStart()){
return;
}else if(rear.getEnd()>=first.getStart()&&
rear.getEnd()<=first.getEnd()){
rear.setEnd(first.getEnd());
rear.setNext(first.getNext());
first=first.getNext();
}else if(rear.getEnd()>first.getEnd()){
rear.setNext(first.getNext());
first=first.getNext();
}
}

}
}

public LinkList intersection(LinkList lis){
LinkList list=new LinkList();
if(head.getNext()==null||lis.getNode().getNext()==null){
head.setNext(null);
return list;
}else{
Node firsta;
Node reara;
Node firstb=lis.getNode().getNext();
Node rearb=lis.getNode();
while(firstb!=null){
firsta=head.getNext();
reara=head;
while(firsta!=null){
if(firstb.getEnd()<=firsta.getStart()){
break;
}else if(firstb.getStart()<=firsta.getStart()&&
firstb.getEnd()<=firsta.getEnd()){
list.union(new Node(firsta.getStart(),firstb.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()<=firsta.getStart()&&
firstb.getEnd()>firsta.getEnd()){
list.union(new Node(firsta.getStart(),firsta.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()>firsta.getStart()&&
firstb.getEnd()<firsta.getEnd()){
list.union(new Node(firstb.getStart(),firstb.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else if(firstb.getStart()>firsta.getStart()&&
firstb.getStart()<firsta.getEnd()&&
firstb.getEnd()>firsta.getEnd()){
list.union(new Node(firstb.getStart(),firsta.getEnd()));
reara=firsta;
firsta=firsta.getNext();
}else{
reara=firsta;
firsta=firsta.getNext();
}
}
rearb=firstb;
firstb=firstb.getNext();
}
}
return list;
}
}
zlbing123 2011-08-18
  • 打赏
  • 举报
回复
mark
x308603129 2011-08-12
  • 打赏
  • 举报
回复
都不会做,学习了
Disa16 2011-08-11
  • 打赏
  • 举报
回复
mark
AndyZhang 2011-08-06
  • 打赏
  • 举报
回复
mark
hehesamalaile 2011-08-05
  • 打赏
  • 举报
回复
07年的帖 都拿出来炒 后继无帖了吗
liuhex 2011-08-05
  • 打赏
  • 举报
回复
先mark
liuhex 2011-08-05
  • 打赏
  • 举报
回复
先mark
liuhex 2011-07-06
  • 打赏
  • 举报
回复
mark
pingdan32 2011-07-04
  • 打赏
  • 举报
回复
看来俺真是个菜鸟
aoyihuashao 2011-07-04
  • 打赏
  • 举报
回复
第一题

byte a1sign[1000000]={false}; 用来存放a1的哪些位有数据

然后遍历a1 为a1sign赋值
然后遍历a2, 如果a2的n位有数据且alsign[n]=ture的话,那结果++
domonate 2011-07-04
  • 打赏
  • 举报
回复
mark
ajige 2011-07-03
  • 打赏
  • 举报
回复
mark mark mark ....
nuptxxp 2011-07-03
  • 打赏
  • 举报
回复
[Quote=引用 117 楼 xxxxxx130 的回复:]

第二题

PHP code
function sortCount($arr, $j){
for ($i = 1; $i < count($arr); $i++){
if ($arr[0] > $arr[$i]){
$j++;
}
}
array_shift($arr);
if (count($arr) ……
[/Quote]
ls时间复杂度达到了O(n^2),但是数据量达到了10^6,还是要更高效的算法
Spring源码解析 2011-07-03
  • 打赏
  • 举报
回复
mark
加载更多回复(115)

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧