找工过程中碰到的笔试面试题整理(2)

继续我的找工笔试面试题整理。

1.获取字符串最长数字串

这个是baidu的一道笔试题,虽然简单,但是要当场写对还是要仔细点。

 

代码
   
     
int getlongestdigital( const char * inputstr, char * outputstr)
{
int start,length,longeststart,longest,offset;
start
= length = longest = longeststart = offset = 0 ;
while ( * inputstr != ' \0 ' )
{
if ( * inputstr >= ' 0 ' &&* inputstr <= ' 9 ' )
{
if (length == 0 )
start
= offset;
length
++ ;
}
else
{
if (length > longest)
{
longest
= length;
longeststart
= start;
}
length
= 0 ;
}
offset
++ ;
inputstr
++ ;
}
if (length > longest)
{
longest
= length;
longeststart
= start;
}
inputstr
-= offset;
for ( int i = 0 ;i < longest;i ++ )
{
outputstr[i]
= inputstr[i + longeststart];
}
outputstr[longest]
= ' \0 ' ;
return longest;
}

 

2.完全二叉树的节点添加

这个也是常出现的题目,完全二叉树是笔试面试经常碰到的,最好要熟悉的它的各种操作。

 

 

代码
    
      
typedef struct TreeNode
{
int data;
struct TreeNode * left;
struct TreeNode * right;
}TreeNode;
void insertnode(TreeNode * root,TreeNode * newnode)
{
queue
< TreeNode *> nonvisit;
nonvisit.push(root);
while ( ! nonvisit.empty())
{
TreeNode
* pvisit = nonvisit.front();
nonvisit.pop();
if (pvisit -> left == NULL)
{
pvisit
-> left = newnode;
return ;
}
nonvisit.push(pvisit
-> left);
if (pvisit -> right == NULL)
{
pvisit
-> right = newnode;
return ;
}
nonvisit.push(pvisit
-> right);
}
}

 

3.链表反序

 

链表是又一个重点,熟悉一下它的各种操作吧

 

代码
    
      
void ReverseList(LinkNode ** root)
{
LinkNode
* temp1 = ( * root) -> next;
if (temp1 == NULL)
return ;
LinkNode
* temp2;
(
* root) -> next = NULL;
temp2
= temp1 -> next;
temp1
-> next = * root;
* root = temp1;
while (temp2 != NULL)
{
temp1
= temp2;
temp2
= temp1 -> next;
temp1
-> next = * root;
* root = temp1;
}
}

 


 

 

 

你可能感兴趣的:(面试题)