题目一、
141. 环形链表(打卡)
# 破坏链表数据、快慢指针都行 # 这里就放置一个大佬的优秀代码,学到了学到了 f = open("user.out", "w") while True: try: param_1 = input() param_2 = int(input()) f.write(‘true\n‘ if param_2>-1 else ‘false\n‘) except: break exit()
题目二、
def detectCycle(self, head: ListNode) -> ListNode: # 牺牲空间换时间 myDict = {} index = 0 while head: if myDict.get(head) is not None: return head else: myDict[head] = index index += 1 head = head.next return None
题目三、
https://leetcode-cn.com/problems/find-the-duplicate-number/ # return sum(nums) - int(len(nums) * (len(nums) - 1) / 2) # 桶记录 visit = [-1 for _ in range(len(nums))] for n in nums: visit[n] += 1 if visit[n] == 1: return n
题目四、
(这里犯了一个最最低级的错误,没有理清楚题意就开整,浪费时间。所以说需求很重要呀)
def firstMissingPositive(self, nums: List[int]) -> int: # 哭了,做了半天题意没理解 # min_n = float(‘inf‘) # max_n = -float(‘inf‘) # for n in nums: # if n > 0: # min_n = min(min_n, n) # max_n = max(max_n, n) # visit = [0 for _ in range(max_n - min_n + 1)] # for n in nums: # if n > 0: # visit[n-min_n] = 1 # for i in range(len(visit)): # if visit[i] == 0: # return min_n+i # return min_n - 1 if min_n > 1 else max_n + 1 # O(4n) -> O(n) lenth = len(nums) if len(nums) == 0: return 1 visit = [0 for _ in range(lenth+1)] for n in nums: if 1 <= n <= lenth: visit[n] = 1 for i in range(1, len(visit)): if visit[i] == 0: return i return lenth+1
LeetCode刷题记录2020-10-09之日常划水!!!
原文:https://www.cnblogs.com/854594834-YT/p/13789706.html