26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
递归实现:
解题思路:
1
.将左子树构造成双链表,并返回链表头节点。
2
.定位至左子树双链表最后一个节点。
3
.如果左子树链表不为空的话,将当前root追加到左子树链表。
4
.将右子树构造成双链表,并返回链表头节点。
5
.如果右子树链表不为空的话,将该链表追加到root节点之后。
6
.根据左子树链表是否为空确定返回的节点。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Convert(self, pRootOfTree):
# write code here
if not pRootOfTree:
return pRootOfTree
if not pRootOfTree.left and not pRootOfTree.right:
return pRootOfTree
self.Convert(pRootOfTree.left)
left = pRootOfTree.left
if left:
while(left.right):
left = left.right
pRootOfTree.left, left.right = left, pRootOfTree
self.Convert(pRootOfTree.right)
right = pRootOfTree.right
if right:
while(right.left):
right = right.left
pRootOfTree.right, right.left = right, pRootOfTree
while(pRootOfTree.left):
pRootOfTree = pRootOfTree.left
return pRootOfTree
27.输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
利用python的itertools,偷懒做法。
class Solution:
def Permutation(self,ss):
result = []
if not ss:
return result
else:
res = itertools.permutation(ss)
for i in res:
if ''.join(i) not in result:
result.append(''.join(i))
return result
T = Solution()
T.Permutation('abc')
方法2:递归算法,注意去掉重复项。
import itertools
class Solution:
def Permutation(self, ss):
# write code here:
if not ss:
return []
if len(ss) == 1:
return list(ss)
charList = list(ss)
Pstr = []
for i in range(len(charList)):
if i > 0 and charList[i] == charList[i-1]:
continue
temp = self.Permutation(ss[:i]+ss[i+1:])
for j in temp:
Pstr.append(charList[i]+j)
return Pstr
28.数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
方法1:利用python模块
import collections
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
c = collections.Counter(numbers)
half = len(numbers)/2
for i in c:
if c[i] > half:
return i
return 0
方法2:数组排序,如果符合条件的数存在,则一定是中间那个数,不过这样的话,复杂度比较高。
# -*- coding:utf-8 -*-
import collections
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here:
if len(numbers) == 0:
return 0
numbers.sort()
count = 0
middle = numbers[len(numbers)/2]
for i in range(len(numbers)):
if numbers[i] == middle:
count += 1
if count > len(numbers)/2 :
return middle
else:
return 0
方法3:
如果有符合条件的数字,则它出现的次数比其他所有数字出现的次数和还要多。在遍历数组时保存两个值:一是数组中一个数字,二是次数。遍历下一个数字时,若它与之前保存的数字相同,则次数加1,否则次数减1;若次数为0,则保存下一个数字,并将次数置为1。遍历结束后,所保存的数字即为所求。然后再判断它是否符合条件即可。
# -*- coding:utf-8 -*-
import collections
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here:
if len(numbers) == 0:
return 0
result = numbers[0]
times = 1
for i in range(len(numbers)):
if times == 0:
result = numbers[i]
times = 1
elif result == numbers[i]:
times += 1
else:
times -= 1
times = 0
for i in range(len(numbers)):
if numbers[i] == result:
times += 1
if times > len(numbers)/2:
return result
else:
return 0
29.输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
方法1:利用快排及列表切片
# -*- coding:utf-8 -*-
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if len(tinput) == 0:
return []
if len(tinput)<k:
return []
tinput.sort()
return tinput[:k]
方法2:利用堆排序,每次弹出堆顶元素
# -*- coding:utf-8 -*-
import heapq
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if not tinput or not k or k >len(tinput):
return []
heapq.heapify(tinput)
return [heapq.heappop(tinput) for _ in range(k)]
30.HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)
class Solution:
def FindGreatestSumOfSubArray(self, array):
if len(array) == 0:
return []
temp_sum = 0
List_sum = []
for i in array:
temp_sum = temp_sum+i
List_sum.append(temp_sum)
if temp_sum > 0: #如果大于0则循环继续累加
continue
else: #如果小于0则置为0再累加
temp_sum = 0
return max(List_sum)