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

刷题记录帖子

🔗
 楼主| Myron2017 2021-12-20 09:24:51 | 只看该作者
全局:
2108. Find First Palindromic String in the Array
2109. Adding Spaces to a String
2110. Number of Smooth Descent Periods of a Stock
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-20 12:42:52 | 只看该作者
全局:
300. Longest Increasing Subsequence

经典题,NlgN 值得背诵
  1. class Solution:
  2.     def lengthOfLIS(self, nums: List[int]) -> int:
  3.         sub = []
  4.         for num in nums:
  5.             i = bisect_left(sub, num)

  6.             # If num is greater than any element in sub
  7.             if i == len(sub):
  8.                 sub.append(num)
  9.             
  10.             # Otherwise, replace the first element in sub greater than or equal to num
  11.             else:
  12.                 sub[i] = num
  13.         
  14.         return len(sub)
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-22 08:38:30 | 只看该作者
全局:
231 Power of two 熟题,主要是记忆 Bit 操作的和 Binary Number of Power of Two 的特征进行求解。
  1. """

  2. n & (n - 1) == 0

  3. """
  4. class Solution(object):
  5.     def isPowerOfTwo(self, n):
  6.         if n == 0:
  7.             return False
  8.         return n & (n - 1) == 0

  9. """

  10. n&(-n) == n

  11. """
  12. class Solution(object):
  13.     def isPowerOfTwo(self, n):
  14.         if n == 0:
  15.             return False
  16.         return n & (-n) == n

  17. """

  18. log N

  19. """
  20. class Solution(object):
  21.     def isPowerOfTwo(self, n):
  22.         if n == 0:
  23.             return False
  24.         while n % 2 == 0:
  25.             n /= 2
  26.         return n == 1
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-27 07:33:46 | 只看该作者
全局:
973. K Closest Points to Origin
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-27 07:42:38 | 只看该作者
全局:
2119. A Number After a Double Reversal

其实很简单,就是最后这个数不能是 10 的倍数。。。
  1. class Solution:
  2.     def isSameAfterReversals(self, num: int) -> bool:
  3.         if num == 0:
  4.             return True
  5.         return num % 10 != 0
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-27 07:43:50 | 只看该作者
全局:
2120. Execution of All Suffix Instructions Staying in a Grid
  1. class Solution:
  2.     def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
  3.         ans = [0] * len(s)
  4.         moves = {'L':(0, -1), 'R':(0, 1), 'U':(-1, 0), 'D':(1, 0)}
  5.         for i in range(len(s)):
  6.             curr_row, curr_col = startPos[0],  startPos[1]
  7.             curr_step = 0
  8.             for j in range(i, len(s)):
  9.                 tmp_row, tmp_col = curr_row + moves[s[j]][0], curr_col + moves[s[j]][1]
  10.                 if 0 <= tmp_row < n and 0 <= tmp_col < n:
  11.                     curr_step += 1
  12.                     curr_row, curr_col = tmp_row, tmp_col
  13.                 else:
  14.                     break
  15.             ans[i] = curr_step
  16.         return ans
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-27 08:47:52 | 只看该作者
全局:
2121. Intervals Between Identical Elements

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-27 13:47:23 | 只看该作者
全局:
476. Number Complement
  1. class Solution:
  2.     def findComplement(self, num: int) -> int:
  3.         str_bin_num = [ch for ch in bin(num)[2:]]
  4.         ans = 0

  5.         for ch in str_bin_num:
  6.             if ch == '0':
  7.                 ans = ans * 2 + 1
  8.             else:
  9.                 ans = ans * 2
  10.         return ans
  11.    
  12.    
  13. """

  14. One bit operations

  15. """

  16. class Solution:
  17.     def findComplement(self, num):
  18.         todo, bit = num, 1
  19.         while todo:
  20.             # flip current bit
  21.             num = num ^ bit
  22.             # prepare for the next run
  23.             bit = bit << 1
  24.             todo = todo >> 1
  25.         return num
  26.    
  27. """

  28. BitMask Operations

  29. """

  30. from math import log2
  31. class Solution:
  32.     def findComplement(self, num):
  33.         # n is a length of num in binary representation
  34.         n = floor(log2(num)) + 1        
  35.         # bitmask has the same length as num and contains only ones 1...1
  36.         bitmask = (1 << n) - 1
  37.         # flip all bits
  38.         return bitmask ^ num
  39.    
  40. """

  41. BitMask Operations with Built-in Functions

  42. """
  43. class Solution:
  44.     def findComplement(self, num):
  45.         return (1 << num.bit_length()) - 1 - num
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-12-29 13:00:56 | 只看该作者
全局:
Leetcode 300. Longest Increasing Subsequence (Python) 重新理解 算法原理
  1. class Solution:
  2.     def lengthOfLIS(self, nums: List[int]) -> int:
  3.         sub = []
  4.         for num in nums:
  5.             i = bisect_left(sub, num)

  6.             # If num is greater than any element in sub
  7.             if i == len(sub):
  8.                 sub.append(num)
  9.             
  10.             # Otherwise, replace the first element in sub greater than or equal to num
  11.             else:
  12.                 sub[i] = num
  13.         
  14.         return len(sub)
复制代码


回复

使用道具 举报

🔗
 楼主| Myron2017 2022-1-17 06:02:04 | 只看该作者
全局:
849. Maximize Distance to Closest Person
回复

使用道具 举报

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

本版积分规则

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