Commit 04f6600c by “安晓东”

假期作业

parent 125d283c
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = [] # 存放所有元素
self.minStack = [] # 存放每一次压入数据时,栈中的最小值(如果压入数据的值大于栈中的最小值就不需要重复压入最小值,小于或者等于栈中最小值则需要压入)
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if not self.minStack or self.minStack[-1] >= x:
self.minStack.append(x)
def pop(self): # 移除栈顶元素时,判断是否移除栈中最小值
"""
:rtype: void
"""
if self.minStack[-1] == self.stack[-1]:
del self.minStack[-1]
self.stack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.minStack[-1]
if __name__ == '__main__':
m = MinStack()
m.push(2)
m.push(3)
m.push(1)
m.push(-1)
# m.push(5)
m.pop()
print(m.stack)
print(m.minStack)
\ No newline at end of file
# 二叉搜索树的最近公共祖先
# 思路:
# 题目中给出是一个二叉搜索树。二叉搜索树中根节点总是比左子树上的每一个数大,总是比右子树每一个数小。
# 因此p和q的公共祖先的值应该介于左子树和右子树之间。从根节点开始遍历,如果根节点的数值均小于p和q的数值,
# 那么代表p和q 的祖先在根节点的右子树中。如果根节点的数值均大于p和q的数值,那么代表p和q 的祖先在根节点的左子树中。
# 若不满足上述,就代表祖先节点就是pq中靠近根节点的一个。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if root == None:
return None
if (root.val > p.val and root.val > q.val) or (root.val < p.val and root.val < q.val):
return root
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left,p,q)
if p.val > root.val and q.val > root.val:
return self.lowestCommonAncestor(root.right,p,q)
return root
if __name__ == '__main__':
s = Solution()
# 思路 :
# 当前结点等于q或者p,那么该结点必为公共结点
# 由于二叉搜索数左小右大的特定,判断当前结点的值与p和q的大小关系,假设p的值比q大。如果当前结点值满足q<value<p,当前结点必为公共结点
# value>p,二叉树向下遍历左子树
# value<q,二叉树向下遍历右子树
class Solution:
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
# """
# if p.val < q.val: # p大,q小
# p, q = q, p
# while root:
# # if root == p or root == q:
# # return root
# # if root.val < p.val and root.val > q.val:
# # return root
# if root.val < q.val:
# root = root.right
# if root.val > p.val:
# root = root.left
# else: # 该句else等价于上面被注释掉的四句
# return root
\ No newline at end of file
# 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
#
# 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
#
# 注意你不能在买入股票前卖出股票。
#
# 示例 1:
#
# 输入: [7,1,5,3,6,4]
# 输出: 5
# 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
# 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
# 示例 2:
#
# 输入: [7,6,4,3,1]
# 输出: 0
# 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) < 2:
return 0
min_price = prices[0]
max_profile = 0
for i in prices:
min_price = min(min_price, i)
max_profile = max(max_profile, i - min_price)
return max_profile
if __name__ == '__main__':
s = Solution()
print(s.maxProfit([9, 1, 4, 6, 7, 3]))
# 合并两个有序链表
# 方法一: 递归
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
# if l1==None and l2==None:
# return None
# if l1==None:
# return l2
# if l2==None:
# return l1
# if l1.val<=l2.val:
# l1.next=self.mergeTwoLists(l1.next,l2)
# return l1
# else:
# l2.next=self.mergeTwoLists(l1,l2.next)
# return l2
if l1 == None and l2 == None:
return None
if l1 == None:
return l2
if l2 == None:
return l1
if l1.val <= l2.val:
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l2,l2.next)
return l2
# 方法二: 非递归
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
# if l1 is None and l2 is None:
# return None
# new_list = ListNode(0)
# pre = new_list
# while l1 is not None and l2 is not None:
# if l1.val < l2.val:
# pre.next = l1
# l1 = l1.next
# else:
# pre.next = l2
# l2 = l2.next
# pre = pre.next
# if l1 is not None:
# pre.next = l1
# else:
# pre.next = l2
# return new_list.next
if l1 is None and l2 is None:
return None
nwe_list = ListNode(0)
pre = nwe_list
while l1 is not None and l2 is not None:
if l1.val < l2.val:
pre.next = l1
l1 = l1.next
else:
pre.next = l2
l2 = l2.next
pre = pre.next
if l1 is not None:
pre.next = l1
else:
pre.next = l2
return nwe_list.next
if __name__ == '__main__':
head1 = ListNode(2)
n1 = ListNode(3)
n2 = ListNode(4)
n3 = ListNode(9)
head1.next = n1
n1.next = n2
n2.next = n3
head2 = ListNode(3)
m1 = ListNode(5)
m2 = ListNode(7)
m3 = ListNode(8)
head2.next = m1
m1.next = m2
m2.next = m3
# while head1:
# print(head1.val)
# head1 = head1.next
# while head2:
# print(head2.val)
# head2 = head2.next
s = Solution()
print(s.mergeTwoLists(head1,head2))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment