查看: 26967| 回复: 229
跳转到指定楼层
上一主题 下一主题
收起左侧

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

   
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 李浩泉 于 2020-7-30 23:38 编辑

年过30,饿不死,吃不饱,对于无背景无天赋的普通大陆华裔男性一代移民来说,如果想年薪30万+美金,读MBA可能性不大还费钱,抱领导粗腿可能性也不大,领导自己都没挣30万呢。

唯一的希望就是刷题进大厂,其他的真的看不到什么可行性。

你们泡妞,我刷题
你们电游,我刷题
你们钓鱼,我刷题
你们游行,我刷题
你们看片,我刷题

1800年代,华工在黑暗的矿坑里淘金
1900年代,华工在黑暗的厨房里刷碗
2000年代,华工在黑暗的小屋里做题

不怨天,不怨地,只怪我爸不努力,从我做起,扭转家庭的惯性下滑,徐徐图之,愚公移山。

从12万年薪到30万年薪,中间只隔着1000+道LC算法题。干TM的。

评分

参与人数 31大米 +43 收起 理由
libriumcc + 1 赞一个
睡不着的喵 + 2 给你点个赞!
Real1minshen + 1 给你点个赞!
金针鸡丝 + 1 很有用的信息!
1people1world + 1 给你点个赞!

查看全部评分


上一篇:ghc 是什么? 真的有这么火吗?
下一篇:UDP 远程通信 (我的PC 怎么call 你的PC)
推荐
 楼主| 李浩泉 2020-7-31 00:19:03 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-7-31 00:22 编辑
backtouzhaogong 发表于 2020-7-31 00:10
刷题有点动力挺好的,pregame pump talk细节不用深究,能激发斗志就行。
楼主能透露一下此番刷题的频率和 ...

没有频率,也没有顺序,就是平推,从#1 Two Sum开始,一直到最后的#1533        
Find the Index of the Large Integer。一边WFH,一边等待着机会。目前的工作已经干了6年多,闭着眼睛都知道怎么干。商科背景没学过CS,毕业就干SQL,现在是从SQL转算法coding。我记得一次面试的时候,面试官中国老哥问我会编程吗?我说会SQL,他说SQL不算编程,只是复杂一点的EXCEL。哈哈哈。莫欺少年穷,等着。

评分

参与人数 3大米 +3 收起 理由
咿呀咿呀哟 + 1 赞一个
yanliangwu + 1 赞一个
zea7ot + 1 赞一个

查看全部评分

回复

使用道具 举报

全局:
欣赏你“不要怂就是干”的态度,爱你

评分

参与人数 1大米 +1 收起 理由
李浩泉 + 1 加油一起干

查看全部评分

回复

使用道具 举报

全局:
刷题有点动力挺好的,pregame pump talk细节不用深究,能激发斗志就行。
楼主能透露一下此番刷题的频率和顺序吗?
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 00:15:16 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-7-31 00:59 编辑

算法第一计:Brute Force 暴力搜索 - 字典法

简单直接,直接For Loop循环,构筑临时的dic{}存储过程中的变量。

Time complexity 时间复杂度 : Because, for each element, I try to find its complement by looping through the rest of array which takes O(n) time. Therefore, the time complexity is O(n^2).

Space complexity 空间复杂度:O(1)

# 1 Two Sum

直接上循环,把每次的值装入临时创建的字典,直到找到答案,最后返回。

  1. '''
  2. Given an array of integers, return indices of the two numbers such that they add up to a specific target.

  3. You may assume that each input would have exactly one solution, and you may not use the same element twice.

  4. Example:

  5. Given nums = [2, 7, 11, 15], target = 9,

  6. Because nums[0] + nums[1] = 2 + 7 = 9,
  7. return [0, 1].
  8. '''

  9. nums = [2, 7, 11, 15]
  10. target = 9

  11. def twoSum(nums, target):
  12.     dic = {}
  13.     for i, num in enumerate(nums):
  14.         n = target - num
  15.         if n not in dic:
  16.             dic[num] = i
  17.         else:
  18.             return [dic[n], i]

  19. print(twoSum(nums,target))
复制代码






回复

使用道具 举报

全局:
加油加油💪
回复

使用道具 举报

全局:
李浩泉 发表于 2020-07-30 09:19:03
没有频率,也没有顺序,就是平推,从#1 Two Sum开始,一直到最后的#1533        
Find the Index of the Large Integer。一边WFH,一边等待着机会
我最近一直在刷SQL😂😂
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 02:39:55 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-7-31 03:18 编辑

算法第二计:Two Pointers 双指针 (包括:左右指针法,快慢指针法 - 弗洛伊德的乌龟(slow 指针)和兔子(fast 指针))

左右指针法:

# 167 Two Sum II - Input array is sorted
# 1099 Two Sum Less Than K

Complexity analysis

Time complexity : Each of the n elements is visited at most once, thus the time complexity is O(n).

Space complexity : I only use two indexes, the space complexity is O(1).

I can apply brute force directly to get O(n^2) time and O(1) space. But I do not make use of the property where the input array is sorted. 如果可以sorting的话,就可以考虑使用左右双指针来节约时间。这类题如果不用双指针,直接上两个嵌套的loop循环,面试一定会被challange。当使用左右指针法的时候,记得用while loop,不用再用for loop了。

  1. '''
  2. Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1.



  3. Example 1:

  4. Input: A = [34,23,1,24,75,33,54,8], K = 60
  5. Output: 58
  6. Explanation:
  7. We can use 34 and 24 to sum 58 which is less than 60.
  8. '''

  9. A = [34,23,1,24,75,33,54,8]
  10. K = 60

  11. def twoSumLessThanK(A, K):
  12.     n = -1
  13.     A.sort()
  14.     left, right = 0, len(A) -1
  15.     while left < right:
  16.         if A[left] + A[right] < K:
  17.             n = max(n, A[left] + A[right])
  18.             left += 1
  19.         else:
  20.             right -= 1
  21.     return n

  22. print(twoSumLessThanK(A, K))
复制代码


[/i]
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 03:18:32 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-7-31 04:01 编辑

左右指针法:

# 167 Two Sum II - Input array is sorted

  1. '''
  2. Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

  3. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

  4. Note:

  5. Your returned answers (both index1 and index2) are not zero-based.
  6. You may assume that each input would have exactly one solution and you may not use the same element twice.
  7. Example:

  8. Input: numbers = [2,7,11,15], target = 9
  9. Output: [1,2]
  10. Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
  11. '''

  12. numbers = [2,7,11,15]
  13. target = 9

  14. def twoSum(numbers,target):
  15.     left = 0
  16.     right = len(numbers) - 1
  17.     while left < right:
  18.         if numbers[left] + numbers[right] == target:
  19.             return [left+1,right+1]
  20.         elif numbers[left] + numbers[right] < target:
  21.             left += 1
  22.         elif numbers[left] + numbers[right] > target:
  23.             right -= 1
  24.                
  25. print(twoSum(numbers,target))
复制代码

回复

使用道具 举报

全局:
lz有群吗 ds转码+1
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 03:58:46 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-7-31 04:36 编辑

快慢指针法:

# 141. Linked List Cycle

这道题是有典故的,称为:弗洛伊德的乌龟(slow 指针)和兔子(fast 指针),和薛定谔的猫差不多一样有名。1960年代提出来的,距今差不多已经60年了。罗伯特·W·弗洛伊德,美国计算机科学家,1978年图灵奖得主,14岁高中毕业,芝加哥大学少年班,17岁大学毕业,26岁卡耐基梅隆大学计算机副教授,32岁斯坦福正教授,唯一的只有本科学历的教授!

Complexity analysis

Time complexity : Let me denote n as the total number of nodes in the linked list. To analyze its time complexity, I consider the following two cases separately.

  • List has no cycle: The fast pointer reaches the end first and the run time depends on the list's length, which is O(n).
  • List has a cycle: I break down the movement of the slow pointer into two steps, the non-cyclic part and the cyclic part, the worst case time complexity is O(N+K), which is O(n).


Space complexity : O(1). I only use two nodes (slow and fast) so the space complexity is O(1).

  1. '''

  2. Given a linked list, determine if it has a cycle in it.

  3. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.



  4. Example 1:

  5. Input: head = [3,2,0,-4], pos = 1
  6. Output: true
  7. Explanation: There is a cycle in the linked list, where tail connects to the second node.

  8. Input: head = [1], pos = -1
  9. Output: false
  10. Explanation: There is no cycle in the linked list.

  11. '''

  12. # Definition for singly-linked list.
  13. # class ListNode:
  14. #     def __init__(self, x):
  15. #         self.val = x
  16. #         self.next = None

  17. class Solution:
  18.     def hasCycle(self, head: ListNode) -> bool:
  19.         slow = head
  20.         fast = head
  21.         while fast and fast.next :
  22.             fast = fast.next.next
  23.             slow = slow.next
  24.             if slow == fast:
  25.                 return True
  26.         return False
复制代码


当然如果不用双指针,这道题也有其解法,比如:

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.next = None

  6. class Solution(object):
  7.     def hasCycle(self, head):
  8.         """
  9.         :type head: ListNode
  10.         :rtype: bool
  11.         """
  12.         while head:
  13.             if head.val == 'I LOVE MEIZI':
  14.                 return True
  15.             else:
  16.                 head.val = 'I LOVE MEIZI'
  17.             head = head.next
  18.         return False
复制代码


回复

使用道具 举报

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

本版积分规则

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