当前位置:网站首页>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;
}
};
边栏推荐
- 牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
- 100+ data science interview questions and answers Summary - basic knowledge and data analysis
- 【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
- The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
- [force deduction question] two point search: 4 Find the median of two positive arrays
- 【力扣刷题】单调栈:84. 柱状图中最大的矩形
- C语言 头哥习题答案截图
- Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
- Least squares system identification class II: recursive least squares
- [从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
猜你喜欢

基于STM32+华为云IOT设计的云平台监控系统

5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?

I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!

The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
![[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram](/img/75/440e515c82b5613b117728ba760786.png)
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
![[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul](/img/26/c0c8a406ff4ffe0ae37d277f730bd0.png)
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul

Structure the graduation project of actual combat camp

牛客编程题--必刷101之动态规划(一文彻底了解动态规划)

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

What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
随机推荐
Experience in hierarchical debugging of boards and cards
How to implement interface current limiting?
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
Cookie和Session详解
What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
板卡的分级调试经验
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
《软件工程》期末重点复习笔记
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
Redis顺序排序命令
1-12Vmware新增SSH功能
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
STM32F103C8T6实现呼吸灯代码
5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?
Keepalived 实现 Redis AutoFailover (RedisHA)1
QT 5.9.8 installation tutorial
Learn about common functional interfaces
心情不好,我就这样写代码
Arduino UNO + DS1302简单获取时间并串口打印