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

[Leetcode] LC Python 刷题笔记 有志者事竟成

   
🔗
 楼主| 李浩泉 2020-10-12 23:13:23 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-13 00:37:56 | 只看该作者
全局:

字典法 - 509. Fibonacci Number

这道题非常好,利用子函数动态构建字典,主函数查询字典直接返回结果。

  1. '''
  2. The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

  3. F(0) = 0,   F(1) = 1
  4. F(N) = F(N - 1) + F(N - 2), for N > 1.
  5. Given N, calculate F(N).

  6. Example 1:

  7. Input: 2
  8. Output: 1
  9. Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
  10. Example 2:

  11. Input: 3
  12. Output: 2
  13. Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
  14. '''

  15. N = 3

  16. def fib(N):
  17.     if N <= 1:
  18.         return N
  19.     return helper(N)
  20.         
  21. def helper(N):
  22.     cache = {0: 0, 1: 1}
  23.     for i in range(2, N+1):
  24.         cache[i] = cache[i-1] + cache[i-2]
  25.     return cache[N]

  26. print(fib(N))
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-13 02:40:54 | 只看该作者
全局:
字典法 - 219. Contains Duplicate II(类似2SUM NON-SORTED ARRAY,不能用双指针)

  1. '''
  2. Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

  3. Example 1:

  4. Input: nums = [1,2,3,1], k = 3
  5. Output: true
  6. Example 2:

  7. Input: nums = [1,0,1,1], k = 1
  8. Output: true
  9. Example 3:

  10. Input: nums = [1,2,3,1,2,3], k = 2
  11. Output: false
  12. '''
  13. nums = [1,2,3,1,2,3]
  14. k = 2

  15. def containsNearbyDuplicate(nums, k):
  16.     d = {}
  17.     for i in range(len(nums)) :
  18.         if nums[i] in d:
  19.             if i - d[nums[i]] <= k:
  20.                 return True
  21.         d[nums[i]] = i
  22.     return False
  23. print(containsNearbyDuplicate(nums, k))
复制代码


回复

使用道具 举报

🔗
kunlun7 2020-10-13 03:12:54 | 只看该作者
全局:
所以是已经拿到30W了,还是想法?
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-13 05:41:34 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-13 10:46:28 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

🔗
pompeya 2020-10-13 12:38:53 | 只看该作者
全局:
呵呵 楼主加油吧
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-14 05:18:25 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-18 00:51:30 | 只看该作者
全局:
350. Intersection of Two Arrays II

  1. '''
  2. Given two arrays, write a function to compute their intersection.

  3. Example 1:

  4. Input: nums1 = [1,2,2,1], nums2 = [2,2]
  5. Output: [2,2]
  6. Example 2:

  7. Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
  8. Output: [4,9]
  9. '''

  10. def intersect(self, nums1, nums2):
  11.     return list((Counter(nums1) & Counter(nums2)).elements())
复制代码

回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-18 07:24:55 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

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

本版积分规则

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