当前位置:网站首页>Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)
Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)
2022-06-26 16:36:00 【我不是萧海哇~~~~】

如果出现下述两种情况,交易 可能无效:
- 交易金额超过 $1000
- 或者,它和 另一个城市 中 同名 的另一笔交易相隔不超过 60 分钟(包含 60 分钟整)
给定字符串数组交易清单 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"]
提示:
- transactions.length <= 1000
- 每笔交易 transactions[i] 按 “{name},{time},{amount},{city}” 的格式进行记录
- 每个交易名称 {name} 和城市 {city} 都由小写英文字母组成,长度在 1 到 10 之间
- 每个交易时间 {time} 由一些数字组成,表示一个 0 到 1000 之间的整数
- 每笔交易金额 {amount} 由一些数字组成,表示一个 0 到 2000 之间的整数
主要思路:
- 先字符串分割
- 然后根据要求直接暴力枚举
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;
}
};
边栏推荐
- Pybullet robot simulation environment construction 5 Robot pose visualization
- TCP congestion control details | 1 summary
- 若依微服务特殊字符串被过滤的解决办法
- 内存分区模型
- Redis 概述整理
- [learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits
- Greenplum database fault analysis - semop (id=2000421076, num=11) failed: invalid argument
- R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
- Big talk Domain Driven Design -- presentation layer and others
- 5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
猜你喜欢

pybullet机器人仿真环境搭建 5.机器人位姿可视化

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系

我把它当副业月入3万多,新手月入过万的干货分享!

Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged

C语言所有知识点小结

# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍

1-12vmware adds SSH function

Junit单元测试

当一个程序员一天被打扰 10 次,后果很惊人!

Niuke Xiaobai monthly race 50
随机推荐
IAR工程适配GD32芯片
Redis migration (recommended operation process) 1
No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete
Redis顺序排序命令
Cuckoo filter for Chang'an chain transaction
Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
了解下常见的函数式接口
pybullet机器人仿真环境搭建 5.机器人位姿可视化
[learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits
若依如何实现接口限流?
Redis order sorting command
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
Niuke Xiaobai monthly race 50
Keepalived 实现 Redis AutoFailover (RedisHA)1
C语言 头哥习题答案截图
Kept to implement redis autofailover (redisha) 1
IAR engineering adapts gd32 chip
Scala 基础 (二):变量和数据类型
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
[207] several possible causes of Apache crash