【leetcode】1169. Invalid Transactions
题目如下:
A 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 string
transactions[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 <= 1000- Each
transactions[i]takes the form"{name},{time},{amount},{city}"- Each
{name}and{city}consist of lowercase English letters, and have lengths between1and10.- Each
{time}consist of digits, and represent an integer between0and1000.- Each
{amount}consist of digits, and represent an integer between0and2000.
解题思路:题目不难,但是坑多。我的方法是以name为key值,把每个人的交易记录存入字典中,然后遍历每个人的交易记录。每找到一个不合法的记录,再用这个记录和这个人其他所有的记录比较,直到找出所有符合条件的结果为止。
代码如下:
class Solution(object):
def invalidTransactions(self, transactions):
"""
:type transactions: List[str]
:rtype: List[str]
"""
def cmpf(v1,v2):
lv1 = v1.split(',')
lv2 = v2.split(',')
return int(lv1[1]) - int(lv2[1])
transactions.sort(cmp = cmpf) res = [] dic = {} dic_invalid = {} for transaction in transactions:
if transaction == 'lee,158,987,mexico':
pass
n, t, a, c = transaction.split(",")
if int(a) > 1000:
if transaction not in dic_invalid:
res.append(transaction)
dic_invalid[transaction] = 1
if n in dic:
for hn,ht,ha,hc in dic[n]:
if hc != c and (int(t) - int(ht)) <= 60:
tran = hn + ',' + ht + ',' + ha + ',' + hc
if tran not in dic_invalid:
dic_invalid[tran] = 1
res.append(tran)
if transaction not in dic_invalid:
res.append(transaction)
dic_invalid[transaction] = 1
dic[n] = dic.setdefault(n,[]) + [(n,t,a,c)]
return res
【leetcode】1169. Invalid Transactions的更多相关文章
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- oracle存储过程和存储函数
存储过程 1.存储过程简介 下面先来简单介绍一下oracle的存储过程的语法,如下: create or replace procedure Tony_Process ( num in number, ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- app测试和web测试的区别
单纯从功能测试的层面上来讲的话,APP 测试.web 测试 在流程和功能测试上是没有区别的根据两者载体不一样,则区别如下:1.系统结构方面 web项目,b/s架构,基于浏览器的:web测试只要更新了服 ...
- 爬虫二之Requests
requests 实例引入 import requests response = requests.get('https://www.baidu.com') response.status_code ...
- python+selenium模拟键盘输入
from selenium.webdriver.common.keys import Keys #键盘导入类 --------------------------------------------- ...
- Keil共存的方法 - Keil MDK兼容Keil C51,实操可行
记录一下成功使Keil MDK和Keil C51共存的过程! 之前一直用Keil C51开发,最近需要用到ARM9内核的IC,就需要Keil C51和Keil MDK共存.看了一下网上几个教程,方法大 ...
- [DS+Algo] 007 希尔排序及其代码实现
步骤 将数组列在一个表(一行多列)中,按特定的步长进行插入排序 步长从 length/2 到 1,每次除 2 将数组转换至表是为了更好地理解这算法,算法本身还是使用数组进行排序 算法性能 (根据步长序 ...
- [转帖]100G QSFP28与CFP,CFP2,CFP4光模块的比较
100G QSFP28与CFP,CFP2,CFP4光模块的比较 https://www.eefocus.com/etulink/blog/17-09/423967_5c520.html 数据中心网络发 ...
- 关于Pulsar与Kafka
在本系列的Pulsar和Kafka比较文章中,我将引导您完成我认为重要的几个领域,并且对于人们选择强大,高可用性,高性能的流式消息传递平台至关重要.消息传递模型(Messaging model)是用户 ...
- 字符串中的replace方法
String.prototype.replace() 该方法作为字符串中非常常用的方法, 今天我们具体介绍一下它的用法 语法格式 someString.replace(regxp | substr, ...