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