面试标题搜集(5)
面试题目搜集(5)
本博客的《面试题目搜集系列不错》
(1)面试题目搜集1
(2)面试题目搜集2
(3)面试题目搜集3
(4)面试题目搜集4
(5)面试题目搜集5
1.递归打印链表
#include<iostream> #include <cstdio> using namespace std; typedef int Elem; struct ListNode{ Elem nData; ListNode *pNext; ListNode(int data,ListNode *next=NULL):nData(data),pNext(next){} }; void PrintReverseList(ListNode *root) { if(!root) return; PrintReverseList(root->pNext); cout<<root->nData<<" "; }; int main() { ListNode *list3 = new ListNode(3,NULL); ListNode *list2 = new ListNode(2,list3); ListNode *list1 = new ListNode(1,list2); ListNode *pHead = new ListNode (0,list1); PrintReverseList(pHead); system("pause"); return 0; }
#include <iostream> #include <string> using namespace std; typedef int StartIndex; typedef int StrLenght; typedef pair<StartIndex,StrLenght> Result; Result FindContinuousSubStr(const string& str) { int start = 0; int maxLen = 1; int length = 1; for(int i=1; i<str.size();++i){ if(str[i] == str[i-1] + 1){ ++length; } else{ if(length > maxLen){ maxLen = length; start = i - length; } length = 1; } } return make_pair(start,maxLen); } int main() { string str = "ads3s1456789DF3456ld345AA"; Result ret = FindContinuousSubStr(str); for(int i=0; i<ret.second; ++i){ cout<<str[ret.first+i]; } system("pause"); return 0; }
3.编写CString类
class CString{
public:
CString(const char *pStr = NULL){
if(pStr == NULL){
pData = new char;
*pData = '\0';
}
else{
pData = new char[strlen(pStr)+1];
assert(pData != NULL);
strcpy(pData,pStr);
}
}
CString(const CString& other){
pData = new char[strlen(other.pData)];
assert(pData != NULL);
strcpy(pData,other.pData);
}
CString& operator=(const CString& other){
if(&other == this){
CString strTemp(other);
char *pTemp = strTemp.pData;
strTemp.pData = pData;
pData = pTemp;
}
return *this;
}
~CString(){
if(!pData)
delete[] pData;
}
private:
char *pData;
};