您的位置:首页 > 职场人生

腾讯面试题的一些解答

2015-09-20 11:17 302 查看
首先上题目原文网址: http://www.cnblogs.com/liferecord/p/4821338.html?utm_source=tuicool

递归/非递归链表转置

  考试的时候转置链表使用非递归写的,今天写完递归以后发现非递归不会写了(汗。。)。等会给出两种方法转置链表。

  首先,定义链表的结点结构

struct node
{
int val;
node* next;
};


  定义链表类

class LinkedList
{
private:
node* root;
void Reverse1(node* n1, node* n2);
public:
node* getRoot(){return root;}
LinkedList();
void print();
void Reverse1(node *root);
void Reverse2(node *root);
};


  递归和非递归做法的思想大同小异:对于当前结点cur和下一个结点cur->next,将cur->next指向cur完成两个结点的转置,然后通过循环或递归完成整条链的转置。递归做法将长链分解成短链,然后不断反接结点,所以在调用递归时应先对后面的结点进行转置,再转置当前结点。

void LinkedList::Reverse1(node *root)
{
Reverse1(root->next,root->next->next);
}

void LinkedList::Reverse1(node* n1, node* n2)
{
if(n2==nullptr) {
root->next=n1;
return ;
}
Reverse1(n1->next,n2->next);
n2->next=n1;
n1->next=nullptr;
}


void LinkedList::Reverse2(node *root)
{
node* cur=root->next;
node* nxt=cur->next;
while(nxt!=nullptr) {
node* tmp=nxt;
nxt=nxt->next;
tmp->next=cur;
cur=tmp;
}
root->next->next=nullptr;
root->next=cur;
}


  完整代码:

#include<bits/stdc++.h>
using namespace std;

struct node
{
int val;
node* next;
};

class LinkedList
{
private:
node* root;
void Reverse1(node* n1, node* n2);
public:
node* getRoot(){return root;}
LinkedList();
void print();
void Reverse1(node *root);
void Reverse2(node *root);
};

int main()
{
LinkedList l;
l.print();
l.Reverse1(l.getRoot());
l.print();
l.Reverse2(l.getRoot());
l.print();
return 0;
}

LinkedList::LinkedList()
{
root = new node;
node* cur=root;
cur->next=new node;
cur=cur->next;
cur->val=4;
cur->next = new node;
cur=cur->next;
cur->val=3;
cur->next = new node;
cur=cur->next;
cur->val=6;
cur->next = new node;
cur=cur->next;
cur->val=1;
cur->next=nullptr;
}

void LinkedList::print()
{
node* t=root;
t=t->next;
cout<<t->val;
t=t->next;
while(t!=nullptr) {
cout<<"->"<<t->val;
t=t->next;
}
cout<<endl;
}

void LinkedList::Reverse1(node *root)
{
Reverse1(root->next,root->next->next);
}

void LinkedList::Reverse1(node* n1, node* n2)
{
if(n2==nullptr) {
root->next=n1;
return ;
}
Reverse1(n1->next,n2->next);
n2->next=n1;
n1->next=nullptr;
}

void LinkedList::Reverse2(node *root)
{
node* cur=root->next;
node* nxt=cur->next;
while(nxt!=nullptr) {
node* tmp=nxt;
nxt=nxt->next;
tmp->next=cur;
cur=tmp;
}
root->next->next=nullptr;
root->next=cur;
}


View Code

海量数据排序

  大量数据排序通用思路:先Hash将大量数据分解到文件中,再对每个文件进行排序(quick sort),然后对这些有序的文件进行归并排序输出为结果文件。

字符串转为整数

  先上Leetcode中这道题的网址,至今未过(。。): https://leetcode.com/problems/string-to-integer-atoi/

  顺便求教cnblogs的大神"+-2"转化为整数为什么是0。

  以后填坑。。

--------------------2015年9月25日更新----------------------

  终于把atoi那题过掉了。。。

  "+-2"返回0的原因是——作为错误输入,返回0。

  过程中又交了一发,WA,告知" -12a45"应该返回-12,于是把预处理去掉,然后就A了。。

  上代码:

class Solution {
public:
int myAtoi(string str) {
int len=str.length();
int k=1;
long long out=0;
int pos=0;
int cnt=0;  // count the numbers of '+' OR '-'
while(str[pos]==' ') ++pos;
while(str[pos]=='+' ||str[pos]=='-') {
++cnt;
if(cnt>=2) return 0;
if(str[pos]=='-') k=-1;
++pos;
}
while(pos<len && str[pos]>='0' && str[pos]<='9') {
out = out*10 + str[pos]-'0';
++pos;
if(out>2147483648 && k==-1) return -2147483648;
if(out>2147483647 && k==1) return 2147483647;
}
return k*out;
}
};


  PS:前几天学了markdown,今后的随笔应该都是用markdown写了,先期待一下吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: