1.用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution:
def __init__(self):
self.stack1=[]
self.stack2=[]
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
# return xx
if not self.stack2:
while self.stack1:
a=self.stack1.pop()
self.stack2.append(a)
return self.stack2.pop()
2.
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
class Solution:
def __init__(self):
self.stack=[]
self.minstack=[]
def push(self, node):
# write code here
if not self.minstack or node < self.minstack[-1]:
self.minstack.append(node)
return self.stack.append(node)
def pop(self):
# write code here
if self.stack[-1]==self.minstack[-1]:
self.minstack.pop()
return self.stack.pop()
def top(self):
# write code here
return self.stack[-1]
def min(self):
# write code here
return self.minstack[-1]
3.
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
if not pushV or len(pushV)!=len(popV):
return 0
stack=[]
for i in pushV:
stack.append(i)
while len(stack) and stack[-1]==popV[0]:
stack.pop()
popV.pop(0)
if len(stack):
return 0
return 1
4.给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
class Solution:
def maxInWindows(self, num, size):
# write code here
if size == 0 or num == []:
return []
tmp=[]
for i in xrange(len(num)-size+1):
tmp.append(max(num[i:i+size]))
return tmp
5.输入一个链表,从尾到头打印链表每个节点的值。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
l = []
head = listNode
while head:
l.insert(0, head.val)
head = head.next
return l
6.
输入一个链表,输出该链表中倒数第k个结点。
class Solution:
def FindKthToTail(self, head, k):
# write code here
l=[]
while head!=None:
l.append(head)
head=head.next
if k>len(l) or k<1:
return
return l[-k]
7.
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
res = head = ListNode(0)
while pHead1 and pHead2:
if pHead1.val > pHead2.val:
head.next = pHead2
pHead2 = pHead2.next
else:
head.next = pHead1
pHead1 = pHead1.next
head = head.next
if pHead1:
head.next = pHead1
if pHead2:
head.next = pHead2
return res.next
8. 输入两个链表,找出它们的第一个公共结点。
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
# write code here
tmp=[]
node1=pHead1
node2=pHead2
while node1:
tmp.append(node1)
node1=node1.next
while node2:
if node2 in tmp:
return node2
else:
node2=node2.next
9.一个链表中包含环,请找出该链表的环的入口结点。
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
tmp=[]
p=pHead
while p:
if p in tmp:
return p
else:
tmp.append(p)
p=p.next
10. 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
class Solution:
def deleteDuplication(self, pHead):
# write code here
if pHead is None or pHead.next is None:
return pHead
head1=pHead.next
if head1.val != pHead.val:
pHead.next=self.deleteDuplication(pHead.next)
else:
while pHead.val == head1.val and head1.next is not None:
head1=head1.next
if pHead.val != head1.val:
pHead=self.deleteDuplication(head1)
else:
return None
return pHead