面试算法问题(1)

1.链表和数组的区别在哪里?
The elements in an array are stored in a continual space in memory. It's easy to get the value of any element on a specified position of an array. But the complexity of insertion and deletion very high.
In contrast, in a linked list, the nodes are stored in defferent space in memory and linked by the 'next' pointer. The operation of insertion and deletion is very efficient for a linked list. But the acquisition of any element requires the traverse of the linked list.

2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
Using Merge Sort

3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法?
Using Heap Sort or Quick Sort

4.请编写能直接实现strstr()函数功能的代码。
what is the function of strstr()?

5.编写反转字符串的程序,要求优化速度、优化空间。 
void  reverse( char [] s)
{
    
char c;
    
int length=s.length;
    
for(int i=0;i<length/2;i++)
    
{
        c
=s[i];
        s[i]
=s[length-1-i];
        s[length
-1-i]=c;
    }

}

6.在链表里如何发现循环链接?
travel the linked list from one node using two pointer. One pointer go forward by one step, and another by two steps. If the first pointer can catch the second one, there is a circle in the linked list.

7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。
void  reorder( int [] cards)
{
    
for(int i=0;i<cards.length;i++)
    
{
        
int rand=(int)(Math.random()*(cards.length-1));
        
int tmpCard=cards[i];
        cards[i]
=cards[rand];
        cards[rand]
=tmpCard;
    }

}


8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?)
(开始还以为用不了四行,结果发现还真得用四行 -.-)
void long toLong( char [] s)
{
    
int i=0;
    
long number=0;
    
for(; i<s.length && char[i]<='9' && char[i]>='0'; number=number*10+char[i++]-'0');
    
return i==s.length?number:-1;
}


9.给出一个函数来输出一个字符串的所有排列。

void  permute( char [] s)
{
    
char[] result=new char[s.length];
    permute(s, result, 
0);
}


void  permute( char [] s,  char [] result,  int  index)
{
    
if(s.length==index)
    
{
        System.out.println(
new String(result));
        
return;
    }

    
for(int i=0;i<s.length;i++)
    
{
        
if(s[i]!='&')
        
{
            result[index]
=s[i];
            
char tmp=s[i];
            s[i]
='&';
            permute(s, result, index
+1);
            s[i]
=tmp;
        }

    }

}



10.请编写实现malloc()内存分配函数功能一样的代码。

11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。 

int  overlapedStrcat( char [] s1,  char [] s2,  char [] result)
{
  result
=new char[s1.length+s2.length];
  
int index=0;
  
for(int i=0,j=0;j<s2.length;index++)
  
{
    
if(i<s1.length)
    
{
      result[index]
=s1[i];
      
if(s2[j]==s1[i])
      
{
        j
++;
      }

      
else
      
{
        j
=0;
      }

      i
++;
    }

    
else
    
{
      result[index]
=s2[j++];
    }

  }

  
return index;
}


12.怎样编写一个程序,把一个有序整数数组放到二叉树中? 

What do u mean......


13.怎样从顶部开始逐层打印二叉树结点数据?请编程。 

breadth first travel

void  breadthFirstTraversal(Tree tree)
{
  Queue queue
=new Queue((int)Math.pow(2,tree.height()));
  queue.enqueue(tree.root);
  
while(!stack.isEmpty())
  
{
    Node node
=(Node)queue.dequeue();
    System.out.print(node.getValue()
+" ");
    
if(node.leftChild()!=null)
      queue.enqueue(node.leftChild());
    
if(node.rightChild()!=null)
      queue.dequeue(node.rightChild());
  }

}



 14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)?  

recursion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值