楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 2021-4-15 12:53:18 | 只看该作者
全局:
LeetCode 341. Flatten Nested List Iterator

理解数据结构,它在题目中的 input 并没有仔细说明,其实还是 List 的input,所以可以 for ele in LL 去 Loop all elements。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-15 14:06:39 | 只看该作者
全局:
667. Beautiful Arrangement II

这道题目其实超出了 medium 的难度。这题非常反直觉的,利用了 compact match。也就是你其实不需要管可以随意安排的部分,你只要管能产生 k 个 difference 的部分就行了。这样的话,方便你提出公式。

其实就是观察到 k 个 difference 只需要 k+1 个数字,别的多出来的数字,随意就行。


回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-15 23:32:41 | 只看该作者
全局:
329. Longest Increasing Path in a Matrix

经典题目,需要记忆做法,参考了 Tommmy 的解法, https://www.youtube.com/watch?v=tBFzoRJtWjI

Classic Recursive Call with Memorization




回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-16 22:39:54 | 只看该作者
全局:
1209. Remove All Adjacent Duplicates in String II

Clever Using of Stack to Solve Problems


回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-17 03:19:52 | 只看该作者
全局:
1146. Snapshot Array

How to store spare array with snapshot

  1. class SnapshotArray:

  2.     def __init__(self, length: int):
  3.         self.array = defaultdict(defaultdict)
  4.         self.snapID = 0
  5.         self.length = length

  6.     def set(self, index: int, val: int) -> None:
  7.         if index < self.length:
  8.             self.array[self.snapID][index] = val

  9.     def snap(self) -> int:
  10.         self.snapID += 1
  11.         return self.snapID - 1

  12.     def get(self, index: int, snap_id: int) -> int:
  13.         while snap_id >= 0:
  14.             if index in self.array[snap_id]:
  15.                 return self.array[snap_id][index]
  16.             snap_id -= 1
  17.         return 0


  18. # Your SnapshotArray object will be instantiated and called as such:
  19. # obj = SnapshotArray(length)
  20. # obj.set(index,val)
  21. # param_2 = obj.snap()
  22. # param_3 = obj.get(index,snap_id)
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-17 05:42:06 | 只看该作者
全局:
LeetCode 1813. Sentence Similarity III (Python)

简单说就是如何通过两个 deque 检查 0 和 -1 位置的元素和对应的  popleft(), pop() 操作来实现对于 prefix 和 suffix 的查找。



  1. class Solution:
  2.     def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
  3.         s1 = deque(sentence1.split())
  4.         s2 = deque(sentence2.split())
  5.         
  6.         # check prefix
  7.         while s1 and s2 and s1[0] == s2[0]:
  8.             s1.popleft()
  9.             s2.popleft()
  10.         # check suffix
  11.         while s1 and s2 and s1[-1] == s2[-1]:
  12.             s1.pop()
  13.             s2.pop()
  14.         
  15.         return len(s1) * len(s2) == 0

复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-19 22:24:33 | 只看该作者
全局:
Remove Nth Node From End of List        Medium

Two Pointers with dummy nodes

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution:
  7.     def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
  8.         fast = slow = dummy = ListNode(0, head)
  9.         
  10.         for _ in range(n):
  11.             fast = fast.next
  12.         
  13.         while fast.next:
  14.             fast = fast.next
  15.             slow = slow.next
  16.             
  17.         slow.next = slow.next.next
  18.         
  19.         return dummy.next
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-20 02:18:47 | 只看该作者
全局:
377        Combination Sum IV        Medium       

DP for 1 to target



  1. class Solution:
  2.     def combinationSum4(self, nums: List[int], target: int) -> int:
  3.         # dp[i] the # of conbinations to reach i
  4.         # dp[i] = sum( dp[x] ) where x <= i
  5.         # reference: [url]https://www.cnblogs.com/grandyang/p/5705750.html[/url]
  6.         dp = [0] * (target+1)
  7.         dp[0] = 1 # base case
  8.         nums = sorted(nums)
  9.         
  10.         for i in range(1, len(dp)):
  11.             for num in nums:
  12.                 if num <= i:
  13.                     dp[i] += dp[i-num]
  14.                 else:
  15.                     break
  16.         return dp[-1]
  17.         
  18.         
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-20 02:48:46 | 只看该作者
全局:
146 LRU Cache

这道题目有现实意义就是如何实现内存的页面调度,参考这个链接,https://www.cnblogs.com/grandyang/p/4587511.html

当然 Python 有神器,就是 OrderedDict 简直是为这道题目量身打造的,popitem(last = False ) 就是先进先出,这道题目要求的,反过来 last = True 就是 stack。



  1. from collections import OrderedDict

  2. class LRUCache:

  3.     def __init__(self, capacity: int):
  4.         self.capacity = capacity
  5.         self.LRU = OrderedDict()


  6.     def get(self, key: int) -> int:
  7.         if key in self.LRU:
  8.             val = self.LRU[key]
  9.             del self.LRU[key]
  10.             self.LRU[key] = val
  11.             return val
  12.         else:
  13.             return -1

  14.     def put(self, key: int, value: int) -> None:
  15.         if key in self.LRU:
  16.             del self.LRU[key]
  17.             self.LRU[key] = value
  18.         else:
  19.             self.LRU[key] = value
  20.             if len(self.LRU) > self.capacity:
  21.                 self.LRU.popitem(last=False)
  22.             


  23. # Your LRUCache object will be instantiated and called as such:
  24. # obj = LRUCache(capacity)
  25. # param_1 = obj.get(key)
  26. # obj.put(key,value)
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-21 05:07:49 | 只看该作者
全局:
589        N-ary Tree Preorder Traversal        Easy        4        Deque + Popleft + Appendleft + Reverse

个人 感觉这道题目五路如何不能算 easy 其实是 medium 啊。特别是 iterative 的方法其实用到了很多深层次的知识。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表