검지offer 코드

9761 단어
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. 2차원 그룹 찾기
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
",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);
}

좋은 웹페이지 즐겨찾기