c语言sscanf函数的用法是什么
243
2022-11-18
1169. Invalid Transactions**
1169. Invalid Transactions**
transaction is possibly invalid if:
the amount exceeds$1000, or;if it occurs within (and including)60 minutes of another transaction with the same name in a different city.Each transaction stringtransactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
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 <= 1000Eachtransactions[i] takes the form"{name},{time},{amount},{city}"Each{name} and{city} consist of lowercase English letters, and have lengths between1 and10.Each{time} consist of digits, and represent an integer between0 and1000.Each{amount} consist of digits, and represent an integer between0 and2000.
C++ 实现 1
此题差评, 描述不清, 另外我看题也不太认真, 浪费了大量时间. ????????????
读懂题意是关键. 这道题要求最后返回的是 invalid 的交易, 而交易被满足如下条件之一就被定义为 invalid:
本次交易的amount > 1000在本次交易 A 的股票名字和另一次交易 B 的股票名字相同的情况下, 交易发生的地点(city)不同并且交易时间在60 分钟之内.
第二点限制条件可以理解为不能在短时间内跑到另一个城市对同一只股票发生交易.
然而, 要真正理解两个限制条件并写出代码, 我们有一些更深层的问题要问, 这可以从题目给出的示例出发. 第 1 个例子中, 由于不满足第二个条件, 所以两个交易都是 invalid 的. 第 2 个例子, 需要特别注意, 这个例子之所以返回 ["alice,50,1200,mtv"] 这个结果, 一方面我们考虑到第一个条件的限制, 而另一方面, 第二个条件也会对结果产生影响, 原因是输入的两个交易 ["alice,20,800,mtv","alice,50,1200,mtv"] 它们发生的交易地点都是 "mtv", 在同一个城市, 所以即使时间上的差值小于 60, "alice,20,800,mtv" 这个交易仍然是有效的. 假设这个示例的输入是 ["alice,20,800,omg","alice,50,1200,mtv"], 此时返回什么 ? ! 此时返回 ["alice,20,800,omg","alice,50,1200,mtv"], 原因它们不满足第二个条件, (额外的 "alice,50,1200,mtv" 还不满足第一个条件).
下面代码的逻辑是:
parse_transaction 函数中, 使用record 保存每个访问过的 transaction,res 保存 invalid 的 transaction
record 的结构比较复杂, 表示为record[name][city].push_back({time, amount}), 第一个 map 的 key 是股票的 name, 而record[name] 中的 map 的 key 对应的是 city, 而record[name][city] 对应的 vector 保存的是{time, amount} pair.对于当前访问的 transaction, 我们看name 是否已经存在于record 中. 不存在就放心的加入到record 中, 因为股票名字name 不同, 不会影响 invalid transaction 的判断.如果record 已经存在了相同的name, 也就是说, 针对name 这只股票, 应该有交易发生了. 我们这时候就要判断它是否会违反第二个约束条件. 注意, 这里暂时不考虑第一个约束条件, 因为不管 amount 有没有超过 1000, 该交易都是要存储到 record 中!这一点需要切记.遍历name 这只股票发生交易的所有城市for (auto &city_record : record[name]),只有当城市不同的时候, 才需要进行时间上的比较.使用invalid 这个 bool 值来判断是否需要将本次交易设置为 invalid 的. 如果上一步的时间比较发现, 存在交易 B 和本次交易 A 发生的地点不同, 但是时间相差不超过 60, 那么不仅需要将交易 B 存入 invalid 交易集合res 中, 当比较完所有的交易后, 还要记得将本次交易 A 存入res 中.之后, 如果invalid == true 或者本次交易的amount > 1000, 那么就将本次交易加入res 中.最后注意, 访问的每次交易都要存入record 中!
class Solution {private: void parse_transaction(const string &transaction, unordered_map
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~