如果出现下述两种情况,交易 可能无效:
给定字符串数组交易清单 transaction 。每个交易字符串 transactions[i] 由一些用逗号分隔的值组成,这些值分别表示交易的名称,时间(以分钟计),金额以及城市。
返回 transactions,返回可能无效的交易列表。你可以按 任何顺序 返回答案。
示例 1:
输入:transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
输出:["alice,20,800,mtv","alice,50,100,beijing"]
解释:第一笔交易是无效的,因为第二笔交易和它间隔不超过 60 分钟、名称相同且发生在不同的城市。同样,第二笔交易也是无效的。
示例 2:
输入:transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
输出:["alice,50,1200,mtv"]
示例 3:
输入:transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
输出:["bob,50,1200,mtv"]
提示:
主要思路:
Code:
class Solution {
public:
void str_split(const std::string & src, const std::string & sep, std::vector<string> & vec_str)
{
std::string::size_type start = 0;
int i=0;
for(std::string::size_type end = src.find(sep, start); end != std::string::npos; end = src.find(sep, start))
{
if(end > start)
{
string str=src.substr(start, end - start);
vec_str.push_back(str);
}
start = end + sep.length();
}
if(start < src.length())
{
string str=src.substr(start, src.length() - start);
vec_str.push_back(str);
}
}
vector<string> invalidTransactions(vector<string>& transactions) {
vector<string>res;
vector<vector<string>>vec;
for(int i=0;i<transactions.size();i++)
{
vector<string>temp;
str_split(transactions[i],",",temp);
vec.push_back(temp);
}
map<string,int>mymap;
for(int i=0;i<vec.size();i++)
{
vector<string>sub=vec[i];
if(atoi(sub[2].c_str())>1000)
{
string str=sub[0]+","+sub[1]+","+sub[2]+","+sub[3];
// res.push_back(str);
mymap.insert(pair<string,int>(str,0));
}
else
{
for(int j=0;j<vec.size();j++)
{
if(j==i)
continue;
vector<string>sub2=vec[j];
if(sub2[3]!=sub[3])
{
if(sub2[0]==sub[0])
{
if(abs(atoi(sub[1].c_str())-atoi(sub2[1].c_str()))<=60)
{
string str=sub2[0]+","+sub2[1]+","+sub2[2]+","+sub2[3];
string str2=sub[0]+","+sub[1]+","+sub[2]+","+sub[3];
mymap.insert(pair<string,int>(str,0));
mymap.insert(pair<string,int>(str2,0));
}
}
}
}
}
}
map<string,int>::iterator it;
for(it=mymap.begin();it!=mymap.end();++it)
{
int cnt=count(transactions.begin(),transactions.end(),it->first);
for(int j=0;j<cnt;j++)
res.push_back(it->first);
}
return res;
}
};