Leetcode 1169:查询无效交易

题目描述

如果出现下述两种情况,交易 可能无效:

交易金额超过 ¥1000
或者,它和另一个城市中同名的另一笔交易相隔不超过 60 分钟(包含 60 分钟整)
每个交易字符串 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 之间的整数

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/invalid-transactions
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

 

解题思路

class Solution {
public:
    static void func(string &str,string &name,int &time,int &amount,string &city){
        string s = "";
        string::size_type pre = str.find_first_not_of(",") , post = str.find_first_of(",");
        s = str.substr(pre,post-pre);
        name = s;
        
        pre = post+1;
        post = str.find_first_of(",",pre);
        s = str.substr(pre,post-pre);
        time = atoi(s.c_str());
        
        pre = post+1;
        post = str.find_first_of(",",pre);
        s = str.substr(pre,post-pre);
        amount = atoi(s.c_str());
        
        pre = post+1;
        s = str.substr(pre,str.length()-pre);
        city = s;
    }
    vector<string> invalidTransactions(vector<string>& trans) {
        int len = trans.size();
        if(len == 0) return trans;
        set<string> st;
        vector<pair<string,string>> strs;
        vector<pair<int,int>> ints;
        string name,city;
        int time,amount;
        for(auto it : trans){
            func(it,name,time,amount,city);
            strs.push_back(make_pair(name,city));
            ints.push_back(make_pair(time,amount));
        }
        for(int i=0;i<len;++i){
            name = strs[i].first;city = strs[i].second;
            time = ints[i].first;amount = ints[i].second;
            for(int j=0;j<len;++j){
                if(ints[j].second > 1000) st.insert(trans[j]);
                if(name == strs[j].first && city != strs[j].second && abs(time - ints[j].first) <= 60){
                    st.insert(trans[i]);
                    st.insert(trans[j]);
                }
            }
        }
        vector<string> ans(st.begin(),st.end());
        return ans;
    }
};

版权声明:本文为weixin_35338624原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。