C++面试 常见手撕代码

1、冒泡排序

#include <iostream>
using namespace std;

void swap(int &a, int &b){
	int temp = a;
	a = b;
	b = temp;
}
void sort(int nums[], int num){

	for (int i = 0; i < num; i++){
		for (int j = num - 1; j >= i; j--){
			if (nums[j] < nums[j - 1]){
				swap(nums[j], nums[j - 1]);
			}
		}
	}
}
void sort2(int nums[],int num){//优化算法
	bool flag=true;
	for(int i=0;i<num&&flag;i++){
		flag=false;
		for(int j=num-1;j>=i;j--){
			if(nums[j]<num[j-1]){
				swap(nums[j],nums[j-1]);
				flag=true;
			}
		}
	}
}
int main()
{
	int a[5] = { 2, 1, 4, 5, 3 };
	sort(a, 5);
	for (int i = 0; i < 5; i++)
	{
		cout << a[i] << endl;
	}
	return 0;
}

2、反转链表

//就地逆置法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *new_head=NULL;//指向新链表头结点的指针
		while(head){
		ListNode *next=head->next;//备份head->next
		head->next=new_head;//更新head->next
		new_head=head;//移动new_head
		head=next;//遍历链表
    }
	return new_head;//返回新链表头结点
};

//头插法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
		ListNode temp_head(0);
		while(head){
			ListNode *next=head->next;
			head->next=temp_head.next;
			temp_head.next=head;
			head=next;
		}
		return temp_head.next;
	}
};

3、二叉树遍历

typedef struct Node {
	char data;
	struct Node *lchild;
	struct Node *rchild;
} *Tree;

//深度优先遍历
void depthFirstSearch(Tree root){
	stack<Node*>nodeStack;
	nodeStack.push(root);
	Node *node;
	while (!nodeStack.empty()){
		node = nodeStack.top();
		cout << node->data;
		nodeStack.pop();
		if (node->rchild){
			nodeStack.push(node->rchild);
		}
		if (node->lchild){
			nodeStack.push(node->lchild);
		}
	}
}

//广度优先遍历
void breadthFirstSearch(Tree root){
	queue<Node*>nodeQueue;
	nodeQueue.push(root);
	Node*node;
	while (!nodeQueue.empty()){
		node = nodeQueue.front();
		nodeQueue.pop();
		cout << node->data;
		if (node->lchild){
			nodeQueue.push(node->lchild);
		}
		if (node->rchild){
			nodeQueue.push(node->rchild);
		}
	}
}

4、大数相加

#include <iostream>
#include <string>
using namespace std;
string plusfun(string num1, string num2){
	if (num1.size()<num2.size()){//把num1固定为位数较大的那个数,方便后面处理
		string temp = num1;
		num1 = num2;
		num2 = temp;
	}
	int length1 = num1.size(), length2 = num2.size(), flag = 0, a, b, sum;//flag是进位标记
	while (length1>0){//从低位开始把对应的位相加
		a = num1[length1 - 1] - '0';//获取num1当前位的数字
		if (length2 > 0)//如果num2还没加完(注意,num2是位数较少的)
			b = num2[length2 - 1] - '0';//获取num2当前位的数字
		else
			b = 0;//如果num2加完了,num2对应位上就没有数来加了
		//这时我没有break,因为虽然num2没有数字来加了,但可能还有进位需要加
		sum = a + b + flag;//num1与num2对应位上的数字相加,再加上进位位
		if (sum >= 10){//如果加起来大于于10,那就需要进位了
			num1[length1 - 1] = '0' + sum % 10;//计算加完之后,当前位应该是多少
			flag = 1;//把进位标记置1
		}
		else{
			num1[length1 - 1] = '0' + sum;//计算加完之后,当前位应该是多少
			flag = 0;//把进位标记置0
		}
		length1--;//向高位移动1位
		length2--;//向高位移动1位
	}
	//如果两个数对应位都加完了,进位位是1,说明位数要增加1了
	//比如99+1,加完之后,变成了三位数100,其实就是再在前面加一位1
	if (1 == flag)
		num1 = "1" + num1;
	return num1;
}
int main()
{
	string num1;
	string num2;
	string result;
	while (cin >> num1 >> num2){
		cout << "num1:" << num1.c_str() << endl;
		cout << "num2:" << num2.c_str() << endl;
		result = plusfun(num1, num2);
		cout << "sum:" << result.c_str() << endl;
	}
	return 0;
}

5、大数相乘

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string plusfun(string num1, string num2){
	if (num1.size()<num2.size()){//把num1固定为位数较大的那个数,方便后面处理
		string temp = num1;
		num1 = num2;
		num2 = temp;
	}
	int length1 = num1.size(), length2 = num2.size(), flag = 0, a, b, sum;//flag是进位标记
	while (length1>0){//从低位开始把对应的位相加
		a = num1[length1 - 1] - '0';//获取num1当前位的数字
		if (length2 > 0)//如果num2还没加完(注意,num2是位数较少的)
			b = num2[length2 - 1] - '0';//获取num2当前位的数字
		else
			b = 0;//如果num2加完了,num2对应位上就没有数来加了
		//这时我没有break,因为虽然num2没有数字来加了,但可能还有进位需要加
		sum = a + b + flag;//num1与num2对应位上的数字相加,再加上进位位
		if (sum >= 10){//如果加起来大于于10,那就需要进位了
			num1[length1 - 1] = '0' + sum % 10;//计算加完之后,当前位应该是多少
			flag = 1;//把进位标记置1
		}
		else{
			num1[length1 - 1] = '0' + sum;//计算加完之后,当前位应该是多少
			flag = 0;//把进位标记置0
		}
		length1--;//向高位移动1位
		length2--;//向高位移动1位
	}
	//如果两个数对应位都加完了,进位位是1,说明位数要增加1了
	//比如99+1,加完之后,变成了三位数100,其实就是再在前面加一位1
	if (1 == flag)
		num1 = "1" + num1;
	return num1;
}
string fun(string num1, string num2){
	string result="0";
	vector<string>p;
	if (num1.size() < num2.size()){
		string temp = num1;
		num1 = num2;
		num2 = temp;
	}
	int length1 = num1.size(), length2 = num2.size(), flag = 0, a, b, mult;
	for (int i = length2 - 1; i >= 0; i--){
		b = num2[i] - '0';
		string multiply = num1;
		for (int j = length1 - 1; j >= 0; j--){
			a = num1[j]-'0';
			mult = a*b+flag;
			if (mult >= 10){
				multiply[j] = '0' + mult % 10;
				flag = mult / 10;
			}
			else{
				multiply[j] = '0' + mult;
				flag = 0;
			}
			
		}
		for (int k = 0; k < length2 - 1 - i; k++)
			multiply = multiply + '0';
		p.push_back(multiply);

	}
	for (int i = 0; i < p.size(); i++){
		result = plusfun(result, p[i]);
	}

	return result;
}

int main()
{
	string num1, num2, result;
	cin >> num1 >> num2;
	cout << "num1:" << num1.c_str() << endl;
	cout << "num2:" << num2.c_str() << endl;
	result = fun(num1, num2);
	cout << "mult:" << result.c_str() << endl;
	return 0;
}

6、求子集

class Solution{
public:
	std::vector<std::vector<int>>subsets(std::vector<int>&nums){
		std::vector<std::vector<int>>result;
		std::vector<int>item;
		result.push_back(item);
		generate(0, nums, item, result);
		return result;
	}
private:
	void generate(int i, std::vector<int>&nums,
		std::vector<int>&item,
		std::vector<std::vector<int>>&result){
		if (i>=nums.size()){
			return;
		}
		item.push_back(nums[i]);
		result.push_back(item);
		generate(i + 1, nums, item, result);
		item.pop_back();
		generate(i + 1, nums, item, result);
	}
};

7、最长上升子序列

int fun(std::vector<int>&nums){
	if (nums.size() == 0)
		return 0;
	std::vector<int>dp(nums.size(), 0);
	dp[0] = 1;
	int LIS = 1;
	for (int i = 1; i < nums.size(); i++){
		dp[i] = 1;//原来未写
		for (int j = 0; j < i; j++){
			if (nums[j] < nums[i]&&dp[i]<dp[j]+1)/*nums[j] < nums[i]*/
				dp[i] = dp[j] + 1;
			else
				dp[i] = dp[j];
			if (LIS < dp[i])
				LIS = dp[i];
		}
	}
	return LIS;
}
点赞