剑指offer Python2.7.3 牛客网在线编程通过测试(持续更新)
1. 二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
self.target = target
self.array = array
if self.array == [[]]:
return False
row = len(self.array)
col = len(self.array[0])
if self.target < self.array[0][0] or self.target > self.array[row-1][col-1]:
return False
else:
for num_row in range(row):
if target in self.array[num_row]:
return True
return False
# write code here
2. 替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
self.s = s
s_output=''
if ' ' not in self.s:
return self.s
else:
for string in self.s:
if string ==' ':
s_output += '%20'
else:
s_output += string
return s_output
# write code here
3. 从头到尾打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
# -*- 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
self.listNode = listNode
L = []
head = self.listNode
while head:
L.append(head.val)
head = head.next
L.reverse()
return L
4. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
# write code here
if not pre or not tin:
return None
root = TreeNode(pre[0])
n = tin.index(pre[0])
root.left = self.reConstructBinaryTree(pre[1:n+1], tin[:n])
root.right = self.reConstructBinaryTree(pre[n+1:], tin[n+1:])
#afterList.append(root.value)
return root
5. 用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.s1 = []
self.s2 = []
def push(self, node):
# write code here
self.s1.append(node)
def pop(self):
# return xx
while len(self.s1)!=0:
self.s2.append(self.s1.pop())
pop_item = self.s2.pop()
while len(self.s2)!=0:
self.s1.append(self.s2.pop())
return pop_item
6. 旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
# -*- coding:utf-8 -*-
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if len(rotateArray)==0:
return 0
else:
return sorted(rotateArray)[0]
7. 斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
首先,直接用递归来解决菲波那切数列是不行的,因为时间复杂度是以n的指数的方式递增的。一个想法就是,我们可以先把前一些项(比如前20项)手动计算出来,这样计算后面的项时,需要用到前20项的时候,可以直接查表来得到结果,这样大大减小了时间复杂度。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.List = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
def Fibonacci(self, n):
# write code here
if n<=20:
return self.List[n]
else:
return self.Fibonacci(n-1) + self.Fibonacci(n-2)
当然上述解法有一点暴力,尤其是如果这道题需要计算前100项,可能需要先把前50项都存好,才能满足时间和内存的要求。那么还有一个解法就是直接用斐波那契数列的定义来计算,而不是使用递归的形式。
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
one = 0
two = 1
if n <=1:
return n
for i in range(2, n+1):
res = one + two
one = two
two = res
return res
8. 跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
# -*- coding:utf-8 -*-
from math import floor
class Solution:
def jumpFloor(self, number):
if number==1:
return 1
# write code here
Y = int(floor(number/2)) # 2的个数
# X = number - 2*Y # 1的个数
res = 0
if number %2 == 1:
for y in range(1, Y+1): # 确保 x 和 y 都不为0
x = number - 2 * y
res += self.Combine(x+y, x)
return res + 1 # 对应y为0的情况
else:
for y in range(1, Y): # 确保 x 和 y 都不为0
x = number - 2 * y
res += self.Combine(x+y, x)
return res + 2 # 对应x和y各为0的情况
def Combine(self, xPLUSy,x):
numerator = 1
denominator1 = 1
denominator2 = 1
for i in range(2, xPLUSy+1):
numerator *=i
for j in range(2, x+1):
denominator1 *= j
for j in range(2, xPLUSy-x+1):
denominator2 *= j
return numerator/denominator1/denominator2