您的位置:首页 > Web前端

剑指offer 代码

2015-10-04 11:29 567 查看
1.赋值运算符

class cmystring
{
public:
cmystring (char *pData = NULL);
cmystring (const cmystring& str);
~cmystring(void);
private:
char *m_pData;
}
//赋值运算符
cmystring& cmystring::operator = (const cmystring &str)
{
if(this == &str)
return *this;
delete []m_pData;
m_pData = NULL;
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
return *this;
}


2.单例模式

class CSingleton
{
private:
CSingleton()
{
}
static CSingleton *m_pInstance;
class CGarbo
{
public:
~CGarbo()
{
if(CSingleton::m_pInstance)
delete CSingleton::m_pInstance;
}
};
static CGarbo Garbo;
public:
static CSingleton *GetInstance()
{
if(m_pInstance == NULL)
m_pInstance = new CSingleton();
return m_pInstance;
}
};


3.二维数组查找

bool Find(int *m,int rows,int columns,int number)
{
bool found = false;
if(m!=NULL&&rows>0&&columns>0)
{
int row = 0;
int column = columns - 1;
while(row <rows &&column>=0)
{
if(m[row*columns+column] == number)
{
found = true;
break;
}
else if (m[row*columns+column]>number)
--column;
else
row++;
}
}
return found;
}


4.替换空格

void RepalceBlank(char string[],int length)
{
if(string == NULL && length <= 0)
return;
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(string[i]!='\0')
{
++originalLength;
if(string[i] == ' ')
++numberOfBlank;
++i;
}
int newLength = originalLength + numberOfBlank * 2;
if (newLength > length)
return;
int indexOfNew = newLength;
int indexOfOriginal = originalLength;
while(indexOfOriginal >= 0&& indexOfNew > indexOfOriginal)
{
if(string[indexOfOriginal] ==' ')
{
string[indexOfNew--] = '0';
string[indexOfNew--] = '2';
string[indexOfNew--] = '%';
}
else
string[indexOfNew--] = string[indexOfOriginal];
indexOfOriginal--;
}
}

5.从尾到头打印链表

struct ListNode
{
int m_key;
ListNode *m_next;
};
void PrintListReverse(ListNode* pHead)
{
stack<ListNode*> nodes;
ListNode* pNode = pHead;
while(pNode!=NULL)
{
nodes.push(pNode);
pNode = pNode->m_next;
}
while(!nodes.empty())
{
pNode = nodes.top();
nodes.pop();
}
}


6.O(l)删除链表的结点

void DeleteNode(ListNode **pListHead,ListNode *pDeleted)
{
if(!pListHead || !pDeleted)
return;
if(pDeleted->m_next!=NULL)
{
ListNode *pNext = pDeleted->m_next;
pDeleted->m_key = pNext->m_key;
pDeleted->m_next = pNext->m_next;
delete pNext;
pNext = NULL;
}
else if(*pListHead == pDeleted)
{
delete pDeleted;
pDeleted = NULL;
*pListHead = NULL;
}
else
{
ListNode* pNode = *pListHead;
while(pNode->m_next!=pDeleted)
pNode = pNode->m_next;
pNode->m_next = NULL;
delete pDeleted;
pDeleted = NULL;
}
}
7.树的子树

struct TreeNode
{
int value;
TreeNode* left;
TreeNode* right;
}
bool HasSubtree(TreeNode *r1,TreeNode *r2)
{
bool result = false;
if(r1!=NULL&&r2!=NULL)
{
if(r1->value == r2->value)
result = DoesTree1HaveTree2(r1,r2);
if(!result)
result = HasSubtree(r1->left,r2);
if(!result)
result = HasSubtree(r1->right,r2);
}
return result;
}

bool DoesTree1HaveTree2(TreeNode* r1,TreeNode* r2)
{
if(r2==NULL)
return true;
if(r1==NULL)
return false;
if(r1->value!=r2->value)
return false;
return DoesTree1HaveTree2(r1->left,r2->left)&&DoesTree1HaveTree2(r1->right,r2->right);
}
8.层次遍历

void printtree(TreeNode* pTreeRoot)
{
if(!pTreeRoot)
return;
deque<TreeNode*> dequeTreeNode;
dequeTreeNode.push_back(pTreeRoot);
while(dequeTreeNode.size())
{
TreeNode *pNode = dequeTreeNode.front();
dequeTreeNode.pop_front();
printf("%d ",pNode->value);
if(pNode->left)
dequeTreeNode.push_back(pNode->left);
if(pNode->right)
dequeTreeNode.push_back(pNode->right);
}
}

9.数组中出现次数超过一半

int MoreThanHalfNum(int* numbers, int length)
{
if(numbers == NULL && length <= 0)
return;
int result = numbers[0];
int times = 1;
for(int i = 1; i < length; ++i)
{
if(times == 0)
{
result = numbers[i];
times = 1;
}
else if (numbers[i] == result)
times++;
else
times--;
}
return result;
}
10.最小k个数

int Partition(int data[],int length,int start, int end)
{
if(data == NULL ||length <= 0 || start < 0 || end >= length)
throw new std::exception("invalid parameters");
int index = RandomInRange(start,end);
swap(&data[index],&data[end]);
int small = start -1;
for(index = start;index<end;++index)
{
if(data[index]<data[end])
{
small++;
if(small != index)
swap(&data[index],&data[small]);
}
}
small++;
swap(&data[small],&data[end]);
return small;
}
void GetLeastNumbers(int *input,int n,int *output,int k)
{
if(input == NULL || output == NULL || k > n || n<= 0 || k<=0)
return;
int start = 0;
int end = n - 1;
int index = Partition(intput, n , start , end);
while(index != k-1)
{
if(index > k-1)
{
end = index - 1;
index = Partition(input,n,start,end)'
}
else
{
start = index+1;
index = Partition(input,n,start,end);
}
}
for(int i = 0;i<k;i++)
output[i] = input[i];
}
11.连续子数组的最大和

int FindGreatestSumOfSubArray(int *pData,int nLength)
{
if((pData == NULL) || (nLength <= 0))
return;
int nCurSum = 0;
int nGreatestSum = 0x80000000;
for(int i = 0; i < nLength ; ++i)
{
if(nCurSum <= 0)
nCurSum = pData[i];
else
nCurSum += pData[i];
if(nCurSum > nGreatestSum)
nGreatestSum = nCurSum;
}
return nGreatestSum;
}
12.字符串中第一个出现一次的字符
char FirstNotRepeatingChar(char* pString)
{
if(pString == NULL)
return '\0';
const int tableSize = 256;
unsigned int hashTable[tableSize];
for(unsigned int i = 0; i < tableSize; ++i)
hashTable[i] = 0;
char* pHashKey = pString;
while(*(pHashKey)!='\0')
hashTable[*(pHashKey++)]++;
pHashKey = pString;
while(*pHashKey != '\0')
{
if(hashTable[*pHashKey] == 1)
return *pHashKey;
pHashKey++;
}
return '\0';
}
13.两个链表的第一个公共交点

int GetListLength(ListNode *pHead)
{
unsigned int length = 0;
ListNode *p = pHead;
while(p!=NULL)
{
length++;
p = p->m_next;
}
return length;
}
ListNode *FindFirstCommonNode(ListNode *pHead1,ListNode *pHead2)
{
unsigned int nLength1 = GetListLength(pHead1);
unsigned int nLength2 = GetListLength(pHead2);
int nLengthDif = nLength1 - nLength2;
ListNode* pListHeadLog = pHead1;
ListNode* pListHeadShort = pHead2;
for(int i = 0;i < nLengthDif;i++)
pListHeadLog = pListHeadLog ->m_next;
while((pListHeadLog!=NULL)&&(pListHeadShort!=NULL)&&(pListHeadLog!=pListHeadShort))
{
pListHeadLog = pListHeadLog ->m_next;
pListHeadShort = pListHeadShort->m_next;
}
ListNode *pFirstCommonNode = pListHeadLog;
return pFirstCommonNode;
}

14.数组中出现次数为1的数字

void FindNumsAppearOnce(int data[],int n)
{
int result = 0;
for(int i = 0;i<n;i++)
result^=data[i];
int t;
for(t=1;t<result;t++)
{
if(t&result)
break;
}
int a=0,b=0;
for(int i = 0;i < n; i++)
{
if(t&data[i])
a^=data[i];
else
b^=data[i];
}
printf("%d %d\n",a,b);
}
15.数组中两个数之和为一定值

bool FindNumbersWithSum(int data[],int length,int sum,int *num1,int *num2)
{
bool found = false;
if(length < 1 || num1 == NULL || num2 == NULL)
return found;
int ahead = length - 1;
int behind = 0;
while(ahead > behind)
{
long long curSum = data[ahead]+data[behind];
if(curSum == sum)
{
*num1 = data[behind];
*num2 = data[ahead];
found = true;
break;
}
else if(curSum > sum)
ahead--;
else
behind++;
}
return found;
}
16.翻转单词

void Reverse(char *pBegin,char *pEnd)
{
if(pBegin == NULL || pEnd == NULL)
return;
while(pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
pBegin ++;
pEnd--;
}
}
char *ReverseSentence(char *pData)
{
if(pData == NULL)
return NULL;
char *pBegin = pData;
char *pEnd = pData;
while(*pEnd !='\0')
pEnd++;
pEnd--;
Reverse(pBegin,pEnd);
pBegin = pEnd = pData;
while(*pBegin!='\0')
{
if(*pBegin ==' ')
{
pBegin++;
pEnd++;
}
else if(*pEnd ==' ' || *pEnd == '\0')
{
Reverse(pBegin,--pEnd);
pBegin = ++pEnd;
}
else
pEnd++;
}
return pData;
}
17.约瑟夫环

int LastRemaining(unsigned int n, unsigned int m)
{
if(n<1||m<1)
return -1;
unsigned int i = 0;
list<int> numbers;
for(i = 0;i<n;i++)
numbers.push_back(i);
list<int>::iterator current = numbers.begin();
while(numbers.size() > 1)
{
for(int i = 1;i<m;++i)
{
current++;
if(current == numbers.end())
current = numbers.begin();
}
list<int>::iterator next = ++current;
if(next == numbers.end())
next = numbers.begin();
--current;
numbers.erase(current);
current = next;
}
return *(current);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: