标签:bec jin cti because different 姓名 == ice present
问题:
给定由【姓名,时间,数额,城市】组成的交易信息数组。
求无效交易的数组。
无效:1 or 2满足
1,数额>1000
2,相同姓名,不同城市,时间差<60
Example 1: Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"] Output: ["alice,20,800,mtv","alice,50,100,beijing"] Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too. 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"] Constraints: transactions.length <= 1000 Each transactions[i] takes the form "{name},{time},{amount},{city}" Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10. Each {time} consist of digits, and represent an integer between 0 and 1000. Each {amount} consist of digits, and represent an integer between 0 and 2000.
解法:
姓名相同,使用unordered_map存储,以姓名为索引key,
value为 剩下的3个情报的,map,方便查找。
每读入一条记录,在已经存储的map中寻找到,和本条记录互为 无效交易的时候,
插入结果set,防止重复,因此使用set
?? 注意:读入记录,并进行分割时,使用输入流 istringstream
使用getline方法,根据分隔符 ‘,‘ 来循环取得各个区域内容。
1 for(string& sac:transactions){ 2 //对每一条记录 3 istringstream ss(sac); 4 vector<string> s(4,""); 5 int i=0; 6 while(getline(ss, s[i++], ‘,‘)); 7 }
代码参考:
1 class Solution { 2 public: 3 vector<string> invalidTransactions(vector<string>& transactions) { 4 unordered_map<string, vector<vector<string>>> transct; 5 set<string> res; 6 for(string& sac:transactions){ 7 istringstream ss(sac); 8 vector<string> s(4,""); 9 int i=0; 10 while(getline(ss, s[i++], ‘,‘)); 11 12 if(stoi(s[2])>1000) res.insert(sac);//amount 13 14 for(vector<string> p:transct[s[0]]){//same name: time,amount,city 15 if(abs(stoi(p[0])-stoi(s[1]))<=60 && p[2]!=s[3]){ 16 res.insert(s[0]+","+p[0]+","+p[1]+","+p[2]); 17 if(res.count(sac)==0) res.insert(sac); 18 } 19 } 20 transct[s[0]].push_back({s[1],s[2],s[3]}); 21 } 22 vector<string> ans; 23 for(string r:res) ans.push_back(r); 24 return ans; 25 } 26 };
标签:bec jin cti because different 姓名 == ice present
原文地址:https://www.cnblogs.com/habibah-chang/p/13202148.html