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

刷题记录帖子

🔗
 楼主| Myron2017 2021-5-26 06:51:19 | 只看该作者
全局:
本帖最后由 Myron2017 于 2021-5-26 06:52 编辑

LeetCode 150. Evaluate Reverse Polish Notation (Python)

原理理解了,这里好多 Python 的细节考察啊!~
How to round to Zero + How to get Python string a number either postive or negative



回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-2 14:26:38 | 只看该作者
全局:
LeetCode 695. Max Area of Island (Python)

DFS 经典运用,题目答案比较奇怪,但是简洁。
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-15 12:12:29 | 只看该作者
全局:
1710        Maximum Units on a Truck        Easy
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-15 12:22:32 | 只看该作者
全局:
94        Binary Tree Inorder Traversal        Easy        5        Morris Traversal of O(1) Space
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-15 12:28:05 | 只看该作者
全局:
145        Binary Tree Postorder Traversal        Easy
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-17 06:00:46 | 只看该作者
全局:
22        Generate Parentheses        Medium        5        DFS + Purning

  1. class Solution:
  2.     def generateParenthesis(self, n: int) -> List[str]:
  3.         ans = []
  4.         def backtrack(S = [], left = 0, right = 0):
  5.             if len(S) == 2 * n:
  6.                 ans.append("".join(S))
  7.                 return
  8.             if left < n:
  9.                 S.append("(")
  10.                 backtrack(S, left+1, right)
  11.                 S.pop()
  12.             if right < left:
  13.                 S.append(")")
  14.                 backtrack(S, left, right+1)
  15.                 S.pop()
  16.         backtrack([], 0, 0)
  17.         return ans
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-17 19:46:04 | 只看该作者
全局:
759        Number of Subarrays with Bounded Maximum        Medium        5        half close half open to remove duplicates

  1. class Solution:
  2.     def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
  3.         # pick each ele to be maximum ele
  4.         # Then sliding windows
  5.         res = 0
  6.         for ind,num in enumerate(nums):
  7.             # num itself is in or not
  8.             if left <= num <= right:
  9.                 res += 1
  10.                 # sliding windows
  11.                 # p1, p2 in
  12.                 #   in range 0~len(nums)
  13.                 #   nums(p1) < num
  14.                 #   nums(p2) < num
  15.                 p1, p2 = ind-1, ind+1
  16.                 while p1 > -1 and nums[p1] < num:
  17.                     p1 -= 1
  18.                     res += 1

  19.                 while p2 < len(nums) and nums[p2] <= num:
  20.                     p2 += 1
  21.                     res += 1

  22.                 if p1+1 != ind and p2-1 != ind:
  23.                     res += (ind-p1-1) * (p2-ind-1)
  24.         return res
  25.                
  26.             
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-20 12:36:32 | 只看该作者
全局:
1903        Largest Odd Number in String        Easy

理解下,什么事奇数,其实只用看最后一位。
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-20 12:39:52 | 只看该作者
全局:
1904        The Number of Full Rounds You Have Played

分类讨论,同时掐头去尾来计算时间。

  1. class Solution:
  2.     def numberOfRounds(self, startTime: str, finishTime: str) -> int:
  3.         res = 0
  4.         if startTime < finishTime:
  5.             # not overnight
  6.             s = startTime.split(":")
  7.             f = finishTime.split(":")
  8.             res = 4 * ( int(f[0]) - int(s[0]) - 1)
  9.             t1 = s[1]
  10.             if t1 == "00": res += 1
  11.             if t1 <= "15": res += 1
  12.             if t1 <= "30": res += 1
  13.             if t1 <= "45": res += 1
  14.             
  15.             t2 = f[1]
  16.             if t2 >= "15": res += 1
  17.             if t2 >= "30": res += 1
  18.             if t2 >= "45": res += 1
  19.             
  20.         else:
  21.             # overnight
  22.             # Day 1
  23.             s = startTime.split(":")
  24.             f = finishTime.split(":")
  25.             res = 4 * ( 23 - int(s[0]))
  26.             t1 = s[1]
  27.             if t1 == "00": res += 1
  28.             if t1 <= "15": res += 1
  29.             if t1 <= "30": res += 1
  30.             if t1 <= "45": res += 1

  31.             # Day 2
  32.             res += 4 * (int(f[0]) )
  33.             t2 = f[1]
  34.             if t2 >= "15": res += 1
  35.             if t2 >= "30": res += 1
  36.             if t2 >= "45": res += 1
  37.         return res
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-6-20 12:42:53 | 只看该作者
全局:
LC 1905,  Count Sub Islands

比较经典的题目,还是要检查基本题型的写法,熟悉胖头鱼的思路。

  1. class Solution:
  2.     def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
  3.         res = 0
  4.         nrows = len(grid2)
  5.         ncols = len(grid2[0])
  6.         directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  7.         self.path = []
  8.         # grid1 + grid2 and only search for 2
  9.         # Then in grid2 search if find any island has 1 or 0 is wrong

  10.         for i in range(nrows):
  11.             for j in range(ncols):
  12.                 if grid2[i][j] == 1 and grid1[i][j] == 1:
  13.                     grid2[i][j] = 2
  14.         print(grid2)
  15.         # DFS
  16.         visited = []
  17.         for _ in range(nrows):
  18.             tmp = []
  19.             for _ in range(ncols):
  20.                 tmp.append(0)
  21.             visited.append(tmp)

  22.         def DFS(r, c):
  23.             visited[r][c] = 1
  24.             self.path.append(grid2[r][c])
  25.             for d in directions:
  26.                 tmp_r, tmp_c = r+d[0], c+d[1]
  27.                 if -1 < tmp_r < nrows and -1 < tmp_c < ncols and not visited[tmp_r][tmp_c] and grid2[tmp_r][tmp_c]:
  28.                     DFS(tmp_r, tmp_c)

  29.         for i in range(nrows):
  30.             for j in range(ncols):
  31.                 if not visited[i][j] and grid2[i][j] == 2:
  32.                     self.path = []
  33.                     DFS(i, j)
  34.                     # print(self.path)
  35.                     if not (1 in self.path):
  36.                         res += 1

  37.         return res
复制代码
回复

使用道具 举报

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

本版积分规则

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