1.在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
# -*- coding:utf-8 -*-
#从右上角往左下角遍历
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
if array == []:
return None
if array[0] == []:
return None
col = len(array[0])
row = len(array)
i = 0
j = col - 1
while(i<row and j >=0):
if array[i][j] == target:
return True
else:
if array[i][j] > target:
j = j-1
else:
i = i+1
return None
2.请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
# -*- coding:utf-8 -*-
#遍历,遇到空格就替换
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
res = ''
for item in s:
if item == ' ':
res = res + '%20'
else:
res += item
return res
3.输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
#可用递归、栈等方式
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
lst = []
p1 = listNode
while(p1 != None):
lst.append(p1.val)
p1 = p1.next
return lst[-1::-1]
4.输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
# write code here
if len(pre) == 0:
return None
if len(pre) == 1:
return TreeNode(pre[0])
root = TreeNode(pre[0])
root.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[0:tin.index(pre[0])])
root.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:])
return root
5.把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。
# -*- coding:utf-8 -*-
#先将右侧与rotateArray[0]相等的元素过滤掉,然后用二分法求解
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
n = len(rotateArray)
if n == 0:
return 0
l = 0
r = n - 1
flag = rotateArray[0]
while (n>0 and rotateArray[r] == flag):
r -= 1
if (r <=0):
return flag
while(l < r):
mid = l + (r-l)/2
if rotateArray[mid] > flag :
l = mid+1
else:
r = mid
return rotateArray[l]
6.大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
# write code here
n1 = 0
n2 = 1
j = 1
res = 0
if n == 0:
return n1
if n == 1:
return n2
while(j<n):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
7.一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
# -*- coding:utf-8 -*-
#类似斐波纳切数列 f(n) = f(n-1) + f(n-2)
class Solution:
def jumpFloor(self, number):
# write code here
if number == 0:
return 0
n1 = 0
n2 = 1
j = 0
while(j<number):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
8.一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
# -*- coding:utf-8 -*-
#找规律
class Solution:
def jumpFloorII(self, number):
# write code here
return 2**(number-1)
9.我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
# -*- coding:utf-8 -*-
#仍为斐波纳锲数列 f(n) = f(n-1) + f(n-2)
#f(n-1) 为n步中第一步竖着覆盖的方法
#f(n-2) 为n步中前两步横着覆盖的方法
class Solution:
def rectCover(self, number):
# write code here
if number == 0:
return 0
n1 = 0
n2 = 1
j = 0
while(j < number):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
10.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
lst_ji = []
lst_ou = []
if len(array) == 0:
return []
for i in array:
if i%2 == 0:
lst_ou.append(i)
else:
lst_ji.append(i)
lst_ji.extend(lst_ou)
return lst_ji
11.输入一个链表,输出该链表中倒数第k个结点。
# -*- coding:utf-8 -*-
#先求出链表的长度l,然后让链表从头开始重新走l-k
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindKthToTail(self, head, k):
# write code here
if head == None:
return None
l = 0
curNode = head
while(curNode != None):
l += 1
curNode = curNode.next
if (l<k):
return None
curNode = head
for i in range(l-k):
curNode = curNode.next
return curNode
12.输入一个链表,反转链表后,输出新链表的表头。
# -*- coding:utf-8 -*-
# 先保存当前节点的下一个节点,然后把当前节点指向上一个节点,循环
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead == None:
return None
preNode = None
curNode = pHead
while(curNode!=None):
nextNode = curNode.next
curNode.next = preNode
preNode = curNode
curNode = nextNode
return preNode
13.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
if pHead1 == None:
return pHead2
if pHead2 == None:
return pHead1
p1 = pHead1
p2 = pHead2
flag = 0
while(p1 and p2):
if (flag == 0):
if p1.val > p2.val:
root = p2
curNode = root
p2 = p2.next
flag = 1
else:
root = p1
curNode = root
flag = 1
p1 = p1.next
else:
if p1.val > p2.val:
curNode.next = p2
p2 = p2.next
else:
curNode.next = p1
p1 = p1.next
curNode = curNode.next
if p1 == None:
curNode.next = p2
else:
curNOde.next = p1
return root