头条第二题C++代码,输入处理起来真的难受

#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>

using namespace std;
struct Interval
{
int start;
int end;
Interval(int _start, int _end) {
start = _start;
end = _end;

}
};
vector<Interval> merge(vector<Interval>& intervals) {
if (intervals.empty()) return {};
sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {return a.start < b.start; });
vector<Interval> res{ intervals[0] };
for (int i = 1; i < intervals.size(); i) {
if (res.back().end < intervals[i].start) {
res.push_back(intervals[i]);
}
else {
res.back().end = max(res.back().end, intervals[i].end);
}
}
for (int i = 0; i < res.size(); i ) {
cout << res[i].start<<","<<res[i].end << ((i!=res.size()-1)?";":"");
}
return res;
}
vector<string> SplitString(const std::string& s, const std::string& c)
{
std::string::size_type pos1, pos2;
std::vector<std::string> v;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));

pos1 = pos2 c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
return v;
}
int main() {
int N;
cin >> N;
vector<string>input(N);
for (int i = 0; i < N; i ) {
cin >> input[i];
}
vector<Interval>inputs;
for (int i = 0; i < N; i ) {
vector<string>tmp = SplitString(input[i], ";");
//cout << tmp.size()<<"#";
for (int j = 0; j < tmp.size(); j ) {
vector<string>ele = SplitString(tmp[j], ",");
Interval interval(stoi(ele[0]), stoi(ele[1]));
inputs.push_back(interval);
//cout << interval.start <<" "<< interval.end << endl;
}
}
merge(inputs);
return 0;


}
考试时候还百度了处理输入的函数😥还有VS默认输入是中文的分号,有同学帮我看才发现的,耽误了蛮久,总共AC三道。

#C++工程师##字节跳动#
全部评论
大佬,考试的时候还能百度吗?
点赞 回复 分享
发布于 2018-08-12 14:23
很稳啊大佬,面的什么岗啊
点赞 回复 分享
发布于 2018-08-12 14:05
字符串流贼好用
点赞 回复 分享
发布于 2018-08-12 13:07
膜拜大佬,3道够面试了
点赞 回复 分享
发布于 2018-08-12 13:01

相关推荐

不愿透露姓名的神秘牛友
08-01 18:00
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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