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

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

   
🔗
pengzhao0524 2020-7-31 08:31:46 | 只看该作者
本楼:
全局:
加油!共勉!
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 09:29:32 | 只看该作者
全局:
# 206. Reverse Linked List

Complexity analysis

Time complexity : Assume that n is the list's length, the time complexity is O(n).

Space complexity : O(1).


  1. def reverseList(self, head: ListNode) -> ListNode:
  2.         
  3.         prev_node = None #设之前节点为空
  4.         curr_node = head #设当前节点等于链表头

  5.         while curr_node : #当前节点如果为空则退出循环
  6.             next_node = curr_node.next #下一个节点从当前节点下一位开始:2->3->4->5
  7.             curr_node.next = prev_node #转换指针方向,转换前的输出:1->2->3->4->5,转换后当前节点的输出:1
  8.             prev_node = curr_node #之前节点等于当前节点等于:1
  9.             curr_node = next_node #当前节点等于下一个节点:2->3->4->5
  10.             #由于指针反向,5次循环后退出,prev_node的值:5->4->3->2->1
  11.    
  12.         return prev_node #退出循环输出之前节点:5->4->3->2->1
复制代码




回复

使用道具 举报

🔗
passionlk 2020-7-31 22:36:22 | 只看该作者
全局:
加油,欣赏你,不要怂就是干!!

评分

参与人数 1大米 +1 收起 理由
李浩泉 + 1 对,不BB,干就完了

查看全部评分

回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 22:44:38 | 只看该作者
全局:
算法第三计:Iteration 迭代:it is the repeat process.

算法第四计:Recursion 递归:it calls itself with smaller input values which obtains the result for the current input by applying simple operations to the returned value for the smaller input.


# 92 Reverse Linked List II

Iterative Link Reversa - Complexity Analysis

Time Complexity:Considering the list consists of N nodes, I process each of the nodes at most once, so it is O(N).

Space Complexity:Since I simply adjust some pointers in the original linked list and only use O(1) additional memory for achieving the final result.

基础概念:数字字符串反转
初等难度:全链表反转,数字字符串一部分反转
中等难度:链表的一部分反转,二叉树反转
高级难度:二叉树上某个树枝上的叶子反转

这是一道中等难度链表中一部分需要反转的题目,需要使用递归或者迭代去做。

  1. '''
  2. Reverse a linked list from position m to n. Do it in one-pass.

  3. Note: 1 ≤ m ≤ n ≤ length of list.

  4. Example:

  5. Input: 1->2->3->4->5->NULL, m = 2, n = 4
  6. Output: 1->4->3->2->5->NULL
  7. '''

  8. def reverseBetween(self, head, m, n):
  9.     if not head: return None
  10.     current, prev = head, None
  11.         
  12.     while m > 1:
  13.         prev = current
  14.         current = current.next
  15.         m, n = m - 1, n - 1
  16.     end, start = current, prev
  17.         
  18.     while n:
  19.         first = current.next
  20.         current.next = prev
  21.         prev = current
  22.         current = first
  23.         n -= 1
  24.             
  25.     if start: start.next = prev
  26.     else: head = prev
  27.     end.next = current
  28.     return head
复制代码






回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 22:55:46 | 只看该作者
全局:
passionlk 发表于 2020-7-31 22:36
加油,欣赏你,不要怂就是干!!
对,不BB,干就完了
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 23:28:13 | 只看该作者
全局:
递归:是重复调用函数自身实现循环。

迭代:是函数内某段代码实现循环。

普通循环:循环代码中参与运算的变量同时是保存结果的变量,当前保存的结果作为下一次循环计算的初始值。

下面这几道题,我都是用Recursion递归构建子函数去做的,和LC的标准方法不一样。

# 38. Count and Say


  1. def countAndSay(self, n):
  2.         if n == 1: return '1'
  3.         if n == 2: return '11'
  4.         s = '11'
  5.         for i in range(2,n):
  6.             s = helper(s)
  7.         return s
  8.    
  9. def helper(s):
  10.     a = s[0]
  11.     for i in range(1,len(s)):
  12.         if s[i] != s[i-1]:
  13.             a = a + ',' + str(s[i])
  14.         else:
  15.             a = a + str(s[i])
  16.     b = a.split(',')
  17.     s = ''
  18.     for j in range(len(b)):
  19.         s = s + str(b[j].count(b[j][0])) + b[j][0]
  20.     return s
复制代码


[/i][/i][/i]
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 23:34:41 | 只看该作者
全局:

118. Pascal's Triangle 和 38. Count and Say,这类题都是一样的,凡是下一步操作需要基于上一步结果的问题,都可以考虑用子函数。


# 118 Pascal's Triangle

  1. '''
  2. Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it.

  3. Example:

  4. Input: 5
  5. Output:
  6. [
  7.      [1],
  8.     [1,1],
  9.    [1,2,1],
  10.   [1,3,3,1],
  11. [1,4,6,4,1]
  12. ]
  13. '''

  14. def generate(self, numRows: int) -> List[List[int]]:
  15.     if numRows == 0: return []
  16.     if numRows == 1: return [[1]]
  17.     if numRows == 2: return [[1],[1,1]]
  18.     n = [1,1]
  19.     s = [[1],[1,1]]
  20.     for j in range(2,numRows):
  21.         n = helper(n)
  22.         s.append(n)
  23.     return s
  24.    
  25. def helper(n):
  26.     m = [0] * (len(n) + 1)
  27.     m[0] = n[0]
  28.     m[-1] = n[-1]
  29.     for i in range(1,len(m)-1):
  30.         m[i] = n[i-1] + n[i]
  31.     return m
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-7-31 23:36:34 | 只看该作者
全局:

# 119 Pascal's Triangle II


  1. '''
  2. Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

  3. Note that the row index starts from 0.


  4. In Pascal's triangle, each number is the sum of the two numbers directly above it.

  5. Example:

  6. Input: 3
  7. Output: [1,3,3,1]
  8. '''

  9. def getRow(self, rowIndex: int) -> List[int]:
  10.     if rowIndex == 0: return [1]
  11.     if rowIndex == 1: return [1,1]
  12.     n = [1,1]
  13.     for j in range(1,rowIndex):
  14.         n = helper(n)
  15.     return n
  16.    
  17. def helper(n):
  18.     m = [0] * (len(n) + 1)
  19.     m[0] = n[0]
  20.     m[-1] = n[-1]
  21.     for i in range(1,len(m)-1):
  22.         m[i] = n[i-1] + n[i]
  23.     return m
复制代码


回复

使用道具 举报

🔗
sevengrain 2020-7-31 23:40:53 | 只看该作者
全局:
哈哈 楼主加油!
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-1 03:47:03 | 只看该作者
全局:
二叉树的基础操作和概念

Binary Tree

Binary Search Tree: A Binary Search Tree or "ordered binary tree" is a type of binary tree where the nodes are arranged in order. For each node, all elements in its left sub-tree are less to the node, and all the elements in its right sub-tree are greater than the node.

最基础的操作:二叉树遍历

# 270 Closest Binary Search Tree Value

把二叉树上所有的值遍历一次,找出和目标值最接近的数值。


  1. '''
  2. Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

  3. Note:

  4. Given target value is a floating point.
  5. You are guaranteed to have only one unique value in the BST that is closest to the target.
  6. Example:

  7. Input: root = [4,2,5,1,3], target = 3.714286

  8.     4
  9.    / \
  10.   2   5
  11. / \
  12. 1   3

  13. Output: 4
  14. '''

  15. def closestValue(self, root: TreeNode, target: float) -> int:
  16.     if not root : return []
  17.     result = []  
  18.     current = []
  19.     current.append(root)
  20.     while current :
  21.         next_level = []
  22.         for n in current :
  23.             m1 = abs(target - n.val)
  24.             result.append([m1,n.val])
  25.             if n.left :
  26.                 next_level.append(n.left)
  27.             if n.right :
  28.                 next_level.append(n.right)
  29.             current = next_level
  30.         result.sort()
  31.     return result[0][1]
复制代码



回复

使用道具 举报

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

本版积分规则

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