Day 31 (1): Algorithms interview questions

Class type:

class CMyString{
public:
	CMyString(char* pdata = NULL);
	CMyString(const CMyString & str);
	~CMyString(void);
private:
	char* m_pData;
}

Definition of an assignment operator overloading - Four point Improvement

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;
}

Advanced Edition

- Consider exception safety (Exception Safety)
(when memory allocation fails to ensure CMyString not modified)
(first create a temporary instance, after switching to the original instance)

CMyString& CMyString::operator = (const CMyString &str){
	if(this != &str){
		CMyString strTemp(str);
		
		char* pTemp = strTemp.m_pData;
		strTemp.m_pData = m_pData;
		m_pData = pTemp;
	}
	return *this;
}		

The difference between the pointer array

int GetSize(int data[]){
	return sizeof(data);
}
int _tmain(int argc, _TCHAR* argv[]){
	int data1[] = {1, 2, 3, 4, 5};
	int size1 = sizeof(data1);
	
	int* data2 = data1;
	int size2 = sizeof(data2);

	int size3 = GetSize(data1);
	
	printf("%d, %d, %d", size1, size2, size3);
}	

Two-dimensional array lookup

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

The same memory address

int _tmain(int argc, _TCHAR* argv[]){
	char str1[] = "hello world";
	char str2[] = "hello world";

	char* str3 = "hello world";
	char* str4 = "hello world";

	if(str1 == str2)
		printf("aaa");
	else
		printf("bbb");
		
	if(str3 == str4)
		printf("ccc");
	else
		printf("ddd");

Replace spaces

Convert special characters in a URL into character recognition server

"" 32 is the ASCII code, hexadecimal 0x20 i.e., the space thus replaced with "20%"

  1. In the rear of the cover modify memory string
  2. Create a new string replacement on the basis of
  • a. a pointer to the first end of the string, a pointer to the second end of the string after the replacement,
  • b. sequentially copy the contents of the string until the first pointer encountered first space
  • c. Alternatively the first space into a '% 20', the first pointer is moved forward a frame, the second frame pointer 3 moves forward,
  • d. Copy the character string in turn forward until he came to a space,
  • e. Repeat
// length 为字符串总容量
void ReplaceBlank(char string[], int length){
	if(string == NULL && length <= 0)
		return;
	
	// originalLength 为字符串 string 的实际长度
	int originalLength = 0;
	int numberOfBlank = 0;
	int i = 0;
	while(string[i] != '\0'){
		++ originalLength;
		
		if((string[i] == ' ')
			numberOfBlank ++;
			
		i++;
	}

	// newLength 为把空格替换成 ’%20‘ 之后的长度
	int newLength = originalLength + numberOfBlank * 2;
	if(newLength > length)
		return;
	
	int indexOfOriginal = originalLength;
	int indexOfNew = newLength;
	while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal){
		if(string[indexOfOriginal] == ' '){
			string[indexOfNew-- ] = '0';
			string[indexOfNew-- ] = '2';
			string[indexOfNew-- ] = '%';
		}
		else
			string[indexOfNew-- ] = string[indexOfOriginal];
		indexOfOriginal--;
	}
}		

List

When inserting a node, allocates memory for the new node, the memory allocation is not completed at one time to create lists, but each add a node, a memory allocation, memory is not idle, so that space efficiency

struct ListNode{
	int m_nValue;
	ListNode * m_pNext;
};
Add the end node

Adding a node to the end of the list (PHEAD is a pointer to a pointer)
(to avoid the end of the function, pHead remains a null pointer)

void AddToTail(ListNode** pHead, int value){
	ListNode* pNew = new ListNode();
	pNew->m_nValue = value;
	pNew->m_pNext = NULL;

	if(*pHead == NULL)
		*pHead = pNew;
	else{
		ListNode* pNode = *pHead;

		while(pNode->m_pNext != NULL)
			pNode = pNode->m_pNext;
		
		pNode->m_pNext = pNew;
	}
}		
Delete Node
void RemoveNode(ListNode** pHead, int value){
	if(pHead == NULL || *pHead == NULL)
		return;
	ListNode* pToBeDeleted = NULL;
	if((*pHead)->m_nValue == value){
		pToBeDeleted = *pHead;
		*pHead = (*pHead)->m_pNext;
	}
	else{
		ListNode* pNode = *pHead;
		while(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value)
			pNode = pNode->m_pNext;
		if(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue == value){
			pToBeDeleted = pNode->m_pNext;
			pNode->m_pNext = pNode->m_pNext->m_pNext;
		}
		if(pToBeDeleted != NULL){
			delete pToBeDeleted;
			pToBeDeleted = NULL
		}
	}
}
List print head from the tail
void PrintListReversely_Iteratively(ListNode* pHead){
	stack<ListNode* > nodes;
	ListNode* pNode = pHead;
	while(pNode != NULL){
		nodes.push(pNode);
		pNode = pNode->m_pNext;
	}
	while(!nodes.empty()){
		pNode = nodes.top();
		printf("%d\t", pNode->m_nValue);
		nodes.pop();
	}
}	
// 用递归来实现
void PrintListReversingly_Reversively(ListNode* pHead){
	if(pHead != NULL){
		if(pHead->m_pNext != NULL)
			PrintListReversingly_Reversively(pHead->m_pNext);
		printf("%d\t", pHead->m_nValue);
	}
} 
//可能导致函数调用栈溢出
He published 182 original articles · won praise 101 · Views 200,000 +

Guess you like

Origin blog.csdn.net/lancecrazy/article/details/100922064