‘’’
11、题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
‘’’
class Solution:
def Power(self, base, exponent):
if base==0:
return 0
if exponent==1:
return 1
else:
if exponent<0:
result = base**(-exponent)
return 1/result
else:
return base**exponent
‘’’
13、题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,
使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
‘’’
class Solution:
def reOrderArray(self, array):
odd = []
double = []
n = len(array)
for j in range(n):
i = j
while i>0:
if array[i]<array[i-1]:
array[i],array[i-1]=array[i-1],array[i]
else:
break
for k in range(n):
if array[k] % 2 == 0:
double.append(array[k])
else:
odd.append(array[k])
return odd+double
‘’’
14、题目描述
输入一个链表,输出该链表中倒数第k个结点。
‘’’
class Solution:
def FindKthToTail(self, head, k):
result = []
while head:
result.append(head)
head = head.next
if k>len(result) or k==0:
return []
else:
return result[-k]
‘’’
15、题目描述
输入一个链表,反转链表后,输出新链表的表头。
‘’’
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
lis = []
pRes = ListNode(-1)
pRes.next = pHead
if not pHead:
lis = []
while pHead:
lis.append(pHead.val)
pHead = pHead.next
newList = lis[::-1]
temp = pRes.next
for i in range(len(newList)):
pRes.next.val = newList[i]
pRes = pRes.next
return temp``