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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章