ZOOM 9月22日 笔试 C++

第一题AC
问题:求输入一个target 和一行整数数组,求两个相加为target的坐标
#include <iostream>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <string>

using namespace std;

vector<int> twoSum(vector<int>& nums, int target) {
	vector<int> res;
	unordered_map<int, int> hash;
	for (int i = 0; i < nums.size(); ++i) {
		int another = target - nums[i];
		if (hash.count(another)) {
			res = vector<int>({ hash[another], i });
			return res;
		}
		hash[nums[i]] = i;
	}
	return res;
}
vector<int> my_split(const string &str, const char flag = ' ')
{
	vector<int> res;
	res.clear();
	istringstream isstr(str);
	string temp;

	while (getline(isstr, temp, flag))
	{
		res.push_back(atoi(temp.data()));
	}
	return res;
}
int main()
{
	int target;
	cin >> target;
	char c;
	c = cin.get();
	string  des_line;
	getline(cin, des_line);
	vector<int> des = my_split(des_line);
	vector<int>res = twoSum(des, target);
	cout << res[0]-1 << " " << res[1]-1 << endl;
	system("pause");
	return 0;
}
第二题提交的时候没考虑path可能不存在,通过80%。做完优化之后本地AC
问题:字符串分隔问题
输入:
    https://zoom.us/main?mno=123456
输出:   protocol = https
    host = zoom.us
    path = main    //可能存在可能不存在
    parameter = mno = 123456

#include <iostream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;


vector<string> my_split(const string &str)
{
	vector<string> res;
	istringstream isstr(str);
	string part,temp;

	// protocol = https
	part += "protocol=";
	getline(isstr, temp, ':');
	part += temp;
	res.push_back(part);
	temp.clear();
	part.clear();


	isstr.get();
	isstr.get();

	//zoom.us/main
	getline(isstr, temp, '?');
	int pos = temp.find('/');
	if (pos == temp.npos)
	{
		part += "host=";
		part += temp;
		res.push_back(part);
		temp.clear();
		part.clear();
	}
	else
	{
		part += "host=";
		for (int i = 0; i < pos; i++)
		{
			part += temp[i];
		}
		res.push_back(part);
		part.clear();

		part += "path=";
		for (int i = pos; i < temp.size(); i++)
		{
			part += temp[i];
		}
		res.push_back(part);
		temp.clear();
		part.clear();
	}
	
	part += "parameter=";
	getline(isstr, temp);
	part += temp;
	res.push_back(part);
	temp.clear();
	part.clear();

	return res;
}

int main()
{

	string des;
	getline(cin, des);
	vector<string> res = my_split(des);
	for (auto start : res)
	{
		cout << start << endl;
	}
	system("pause");
	return 0;
}



#题解##C++工程师#
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 12:05
点赞 评论 收藏
分享
07-11 15:12
门头沟学院 Java
别人在上班,我就在工位上看看视频啥的,这正常吗?
程序员小白条:实习就是摸鱼,只是公司指标,把你进来了,可能那时候客户很多,但等你进来的时候,已经是淡季了,根本没多少需求,或者说根本不适合实习生去完成,因此你就每天干坐着就行,可能1,2个月都没需求
实习生的蛐蛐区
点赞 评论 收藏
分享
写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
点赞
11
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务