Commit c68264f9 by ligang

'春节作业'

parent fd89f9fe
#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:
“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,
满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:输入: root = [6,2,8,0,4,7,9,null,null,3,5],
p = 2, q = 8 输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2,
因为根据定义最近公共祖先节点可以为节点本身。 说明:所有节点的值都是唯一的。p、q 为不同节点且均存在于给定的二叉搜索树中。
思路:
这题可以通过递归来调用
1、如果给出的结点大于p. 小于q 那么这个数就为给出p,q 的根结点。
2、如果给出的结点为大于p,q 那么这个结点在左子数
3、如果给出的结点为小于p,q 那么这个结点在右子数
"""
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if root is None:
return None
elif root.value > max(p,q):
return self.lowestCommonAncestor(root.left,p,q)
elif root.value < min(p,q):
return self.lowestCommonAncestor(root.right,p,q)
else:
return root.value
#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
给定一个数组,它的第 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。
思路:追求最大利润化就是两两相减求最大值
"""
#方法一
#测试可以得出结果,但时间复杂度较高 O(n2)
class Solution:
def maxProfit(self, prices):
l = len(prices)
maxVal = 0
for i in range(l-1):
for j in range(i+1,l):
val = prices[j] - prices[i]
if val < 0:
val = 0
maxVal = max(maxVal, val)
return maxVal
#方法2 时间复杂度O(n)
def maxProfit2(self, prices):
rs = 0
start_val = prices[0]
for i in prices:
rs = max(rs,i-start_val)
start_val = min(start_val,i)
return rs
lst = [7,1,5,3,6,4]
s = Solution()
r = s.maxProfit(lst)
print(r)
rs = s.maxProfit2(lst)
print(rs)
\ No newline at end of file
#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
while True:
new_node = ListNode(0) #建立新结点
pre_node = new_node
while l1 and l2:
if l1.val < l2.val:
pre_node.next = l1
l1 = l1.next
else:
pre_node.next = l2
l2 = l2.next
pre_node = pre_node.next
if l1 is not None:
pre_node.next = l1
if l2 is not None:
pre_node.next = l2
#print(pre_node.val)
return new_node.next
head1 = ListNode(2)
n1 = ListNode(8)
n2 = ListNode(9)
n3 = ListNode(10)
head1.next = n1
n1.next = n2
n2.next = n3
#while head1:
# print(head1.val)
# head1 = head1.next
head2 = ListNode(4)
m1 = ListNode(5)
m2 = ListNode(6)
m3 = ListNode(10)
head2.next = m1
m1.next = m2
m2.next = m3
#while head2:
# print(head2.val)
#head2 = head2.next
s = Solution()
r = s.mergeTwoLists(head1,head2)
while r:
print(r.val)
r = r.next
\ No newline at end of file
#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
设计一个支持 push,pop,top 操作,
并能在常数时间内检索到最小元素的栈。
push(x) -- 将元素 x 推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。
示例:MinStack minStack = new MinStack();
minStack.push(-2);minStack.push(0);
minStack.push(-3);minStack.getMin(); --> 返回 -3.
minStack.pop();minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
"""
class MinStack(object):
def __init__(self):
self.lst = []
self.minlst =[]#最小栈元素定义一个0
def push(self, x):
self.lst.append(x)
if len(self.minlst) < 1:
self.minlst.append(x)
if x <= self.minlst[-1] :#压栈元素与当前栈顶元素作对比,小则入栈
self.minlst.append(x)
return True
def pop(self):
if not self.isEmpty():
if self.top() == self.minlst[-1]:
self.minlst.pop()
return self.lst.pop()
#获取栈顶元素
def top(self):
return self.lst[-1]
#获取当前栈中最小元素
def getMin(self):
if not self.isEmpty():
return self.minlst[-1]
#打印
def print(self):
print(self.lst)
def clear(self):
del(self.lst)
def isEmpty(self):
return len(self.lst) < 1
stack = MinStack();
stack.push(-2);
stack.push(0);
stack.push(-3);
stack.push(5);
stack.push(8);
print("入栈的数为:")
stack.print()
print("返回当前栈的最小元素为:")
print(stack.getMin())
print("出栈的数为:")
print(stack.pop())
print("返回前栈顶元素为:")
print(stack.top())
print("清空栈中元素")
stack.clear()
\ No newline at end of file
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