当前位置:网站首页>Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
2022-06-26 16:47:00 【I'm not xiaohaiwa~~~~】

If the following two situations occur , transaction May not be valid :
- Transaction amount exceeds $1000
- perhaps , It and Another city in The same name The other transaction is no more than 60 minute ( contain 60 Minutes )
Given string array transaction list transaction . Each transaction string transactions[i] Consists of values separated by commas , These values represent the name of the transaction , Time ( In minutes ), Amount and city .
return transactions, Returns a list of transactions that may not be valid . You can press In any order Return to the answer .
Example 1:
Input :transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output :["alice,20,800,mtv","alice,50,100,beijing"]
explain : The first deal is void , Because the second transaction is not more than 60 minute 、 The names are the same and take place in different cities . Again , The second deal is also void .
Example 2:
Input :transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output :["alice,50,1200,mtv"]
Example 3:
Input :transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output :["bob,50,1200,mtv"]
Tips :
- transactions.length <= 1000
- Every deal transactions[i] Press “{name},{time},{amount},{city}” The format for recording
- Name of each transaction {name} And the city {city} It's all made up of lowercase letters , The length is in 1 To 10 Between
- Every trading time {time} It's made up of some numbers , It means a 0 To 1000 Integer between
- The amount of each transaction {amount} It's made up of some numbers , It means a 0 To 2000 Integer between
Main idea :
- First split the string
- Then enumerate directly according to the requirements
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;
}
};
边栏推荐
- Natural language inference with attention and fine tuning Bert pytorch
- 构造函数和析构函数
- QT 5.9.8 installation tutorial
- 建立自己的网站(16)
- The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance
- Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
- [chat in 5] eight years after graduation, I have been pursuing my dream
- [机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存
- Count the number of each vowel letter in the string
- C language -- legal identifier and integer
猜你喜欢

Teach you to learn dapr - 8 binding

TCP拥塞控制详解 | 1. 概述

Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)

Constructors and Destructors

Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed

Web3去中心化存储生态图景

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

Research on natural transition dubbing processing scheme based on MATLAB

Science | 红树林中发现的巨型细菌挑战传统无核膜观念

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系
随机推荐
day10每日3题(2):统计最大组的数目
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
MS|谢黎炜组发现混合益生菌制剂及其代谢产物可缓解结肠炎
牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
Teach you to learn dapr - 4 Service invocation
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
Use the array to calculate the average of N numbers, and output the numbers greater than the average
What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
Calculate a=1, a2=1/1=a1
LeetCode Algorithm 24. 两两交换链表中的节点
Scala Basics (II): variables and data types
最小二乘系统辨识课 中篇:递归最小二乘
QT 5.9.8 installation tutorial
Redis migration (recommended operation process)
num[i]++
内存分区模型
Teach you to learn dapr - 3 Run the first with dapr Net program
数字藏品与NFT到底有何区别