富途面试算法题

判断数组是否是出栈顺序

#include <iostream>
using namespace std;
#include <vector>
#include <stack>
//判断数组是否是出栈顺序
bool isStackOutRight(vector<int>&sIn, vector<int>&sOut, int length) {
	//辅助栈
	stack<int>stackdata;
	int index = 0;
	for (int i = 0; i < length; i++) {
		if (!stackdata.empty() && stackdata.top() == sOut[i]) {
			stackdata.pop();
		}
		else {//辅助栈为空,或者不等于首元素,指向压栈操作
			while (index < length && sIn[index] != sOut[i]) {
				stackdata.push(sIn[index++]);
			}
			if (index == length) {
				return false;
			}
			else {
				index++;
			}
		}
	}
	return true;
}

股票的最大利润

//一个数组找收益最大的买入和卖出点(股票的最大利润)
int Maxdiff(vector<int>&v) {
	int length = v.size();
	if (v.empty() || length < 2) {
		return 0;
	}
	int min = v[0];
	int Maxdiff = v[1] - v[0];
	int m = 0, n = 1;
	for (int i = 2; i < length; i++) {
		if (v[i - 1] < min) {
			min = v[i - 1];
			m = i - 1;
		}
		if (v[i] - min > Maxdiff) {
			Maxdiff = v[i] - min;
			n = i;
		}
	}
	cout << m << " " << n << endl;
	return Maxdiff;
}

int main() {
	vector<int>v = { 9,11,8,5,7,12,16,14 };
	int res = Maxdiff(v);
	cout << res << endl;
	system("pause");
	return 0;
}

二叉排序树插入

//二叉排序树插入
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int val) :val(val) {};
};

bool Insert(TreeNode *head, int key) {
	if (head == nullptr) {
		TreeNode *tmp = new TreeNode(key);
		tmp->left = nullptr;
		tmp->right = nullptr;
	}
	if (head->val < key) {
		Insert(head->right, key);
	}
	else if (head->val > key) {
		Insert(head->left, key);
	}
	else {
		return false;
	}
	return true;
}

括号的匹配

bool isValid(string s) {
		map<char, char>mp;
		mp.insert(make_pair(')', '('));
		mp.insert(make_pair('}', '{'));
		mp.insert(make_pair(']', '['));
		stack<char>stk;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
				stk.push(s[i]);
			}
			else if (s[i] == '}' || s[i] == ')' || s[i] == ']') {
				if (stk.top() == mp[s[i]]) {
					stk.pop();
				}
				else {
					return false;
				}
			}
		}
		if (stk.empty()) {
			return true;
		}
	}
};

死锁的例子

#include <iostream>
#include <thread>
#include <mutex>
//#include <unistd.h>
#include <windows.h>
using namespace std;
mutex mt1, mt2;
static int data1 = 1, data2 = 1;
void a2() {
	mt2.lock();
	data1++;
	cout << " a2 " << data1 << endl;
	Sleep(1);
	mt1.lock();  //此时a1已经对mt1上锁,所以要等待
	data2++;
	mt1.unlock();
	mt2.unlock();
}
void a1() {
	mt1.lock();
	data2++;
	cout << " a1 " << data2 << endl;
	Sleep(1);
	mt2.lock();  //此时a2已经对mt2上锁,所以要等待
	data1++;
	mt2.unlock();
	mt1.unlock();
}

int main() {
	//int data = 1;
	thread t2(a2);
	thread t1(a1);
	t1.join();
	t2.join();
	cout << "main here" << endl;  //要t1线程、t2线程都执行完毕后才会执行
	system("pause");
	return 0;
}

随机打乱有序数组

洗牌算法
https://blog.csdn.net/qq_26399665/article/details/79831490

//方法一  抽牌
#include <algorithm>
#include <ctime>
void Fisher_Yates_Shuffle1(vector<int>&nums, vector<int>&res) {
	// 时间复杂度为O(n^2),空间复杂度O(n)
	int k;
	while (!nums.empty()) {
		srand((unsigned)time(NULL));
		k = rand() % nums.size();
		res.push_back(nums[k]);
		nums.erase(nums.begin() + k);
	}
}
int main() {
	vector<int>nums = { 1,2,3,4,5,6,7,8,9 };
	vector<int>res;
	Fisher_Yates_Shuffle1(nums, res);
	for (int i = 0; i < res.size(); i++) {
		cout << res[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

//方法二
#include <algorithm>
#include <ctime>
int main() {
	vector <int>nums = { 1,2,3,4,5,6,7,8,9 };
	srand((unsigned)time(NULL));
	random_shuffle(nums.begin(), nums.end());
	for (int i = 0; i < nums.size(); i++) {
		cout << nums[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

// 洗牌算法
1. 建立一个数组大小为 n 的数组 arr,分别存放 1 到 n 的数值;
2. 生成一个从 0 到 n - 1 的随机数 x;
3. 输出 arr 下标为 x 的数值,即为第一个随机数;
4. 将 arr 的尾元素和下标为 x 的元素互换;
5.2,生成一个从 0 到 n - 2 的随机数 x;
6. 输出 arr 下标为 x 的数值,为第二个随机数;
7. 将 arr 的倒数第二个元素和下标为 x 的元素互换;
   ……
如上,直到输出 m 个数为止
#include <algorithm>
#include <ctime>

int main() {
	vector <int>nums = { 1,2,3,4,5,6,7,8,9 };
	int n = nums.size();
	int k = n - 1;
	while (k >= 1) {
		srand((unsigned)time(NULL));
		int m = rand() % (k+1);
		swap(nums[m], nums[k]);
		k--;
	}
	for (int i = 0; i < n; i++) {
		cout << nums[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

给定一个非负整数的列表,安排它们形成最大的数字

bool compare(int item1, int item2) {
	//使用 to_string 把 int 转换成为 string
	string m = to_string(item1);
	string n = to_string(item2);
	string mn = m + n;
	string nm = n + m;
	return mn > nm; // 〉降序 生成最大的数; < 升序 生成最小的数 
}
string PrintMinNumber(vector<int>nums) {
	string res = "";
	if (nums.size() == 0) {
		return res;
	}
	sort(nums.begin(), nums.end(), compare);
	for (int i = 0; i < nums.size(); i++) {
		res += to_string(nums[i]);
	}
	return res;
}
int main() {
	vector<int>nums = { 2,32,321 };
	string res = PrintMinNumber(nums);
	cout << res << endl;
	system("pause");
	return 0;
}

判断栈的出栈顺序是否正确

#include <iostream>
using namespace std;
#include <vector>
#include <stack>
#include <string>

bool isStackOutRight(vector<int>&sIn, vector<int>sOut, int length) {
	stack<int>stackdata;//辅助栈 需要用到一个辅助栈stackdata来存储压入栈而尚未出栈的元素
	int index = 0;
	for (int i = 0; i < length; i++) {
		if (!stackdata.empty() && stackdata.top() == sOut[i]) {
			stackdata.pop();
		}
		else { //辅助栈为空,或者不等于首元素,指向压栈操作
			while (index < length && sIn[index] != sOut[i]) {
				stackdata.push(sIn[index++]);
			}
			if (index == length) {
				return false;
			}
			else {
				index++;
			}
		}
	}
	return true;
}

原地移除字符串中空格

void trimAllSpace(string &str) {
	string s = " ";
	int pos = str.find(s, 0);
	while (pos != -1) {
		str.erase(pos, 1);
		pos = str.find(s, 0);
	}
}
int main() {
	string str = "fdsf fgfg  ggfdg ";
	trimAllSpace(str);
	cout << str << endl;

	system("pause");
	return 0;
}
  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值