1、面试题16:数值的整数次方
- 题目描述:
实现函数double Power(double base, int exponent),求base的exponent次方、不得使用库函数,同时不需要考虑大数问题。
- 详细代码:
# -*- coding:utf-8 -*-
class Solution:
def Power(self, base, exponent):
# write code here
if base == 0.0 and exponent < 0:
return 0.0
if exponent >= 0:
return self.PowerwithExponent(base, exponent)
else:
return 1.0 / self.PowerwithExponent(base, -exponent)
def PowerwithExponent(self, base, exponent):
if exponent == 0.0:
return 1.0
if exponent == 1.0:
return base
result = self.PowerwithExponent(base, exponeny >> 1)
result *= result
if exponent & 0x1 == 1:
result *= base
return result
2、面试题17:打印从1到最大的n位数
- 题目描述:
输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1、2、3一直到最大的3位数999.
- 详细代码:
# -*- coding:utf-8 -*-
class Solution:
def Print1ToMaxOfNDigits(self, n):
if n <= 0:
return
number = ['0'] * n
while not self.Increment(number):
self.PrintNumber(number)
def Increment(self, number):
isOverflow = False
nTakeover = 0
nLength = len(number)
for i in range(nLength-1, -1, -1):
nSum = int(number[i]) + nTakeover
if i == nLength - 1:
nSum += 1
if nSum >= 10:
if i == 0:
isOverflow = True
else:
nTakeover = 1
nSum -= 1
number[i] = str(nSum)
else:
number[i] = str(nSum)
break
return isOverflow
def PrintNumber(self, number):
isBeginning = True
nLength = len(number)
for i in range(nLength):
if isBeginning and number[i] != 0:
isBeginning = False
if not isBeginning:
print('%c'%number[i])
print('\t')
3、面试题18:删除链表的节点
题目一:在O(1)时间内删除链表节点
- 题目描述:
给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。
- 详细代码:
class ListNode:
def __init__(self):
self.value = None
self.next_node = None
class Solution:
def delete_node(self,head_node,del_node):
"""
删除指定节点
"""
if not (head_node and del_node):
return False
#删除节点在链表中间
if del_node.next_node:
del_node_next = del_node.next_node
del_node.value = del_node_next.value
del_node.next_node = del_node_next.next_node
del_node_next.value = None
del_node_next.noex_node = None
#链表只有一个节点
elif del_node == head_node:
del_node = None
head_node = None
#删除节点是尾节点
else:
node = head_node
while node.next != del_node:
node = node.next_node
node.next_node = None
del_node = None
return head_node
题目二:删除链表中重复的节点
- 题目描述:
在一个排序的链表中,请删除重复的节点,如1-2-3-3-4-4-5在重复的节点被删除后为1-2-5。
- 详细代码:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
first = ListNode(-1)
last = first
first.next = head
while head and head.nex:
if head.val == head.next.val:
val = head.val
while head and head.val == val:
head = head.next
last.next = head
else:
last = head
head = head.next
return first.next
4、面试题19:正则表达式匹配
- 题目描述:
请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配。
- 详细代码:
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
if not p:
return not s
first_match = bool(s) and p[0] in {s[0], '.'}
if len(p) > 1 and p[1] = '*':
return (self.isMatch(s, p[2:]) or first_match and self.isMatch(s[1:], p))
else:
return first_match and self.isMatch(s[1:], p[1:])
5、面试题20:表示数值的字符串
- 题目描述:
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
- 详细代码:
# -*- coding:utf-8 -*-
class Solution:
# s字符串
def isNumeric(self, s):
# write code here
if not s:
return False
alist = [i.lower() for i in s]
if 'e' in alist:
index = alist.index('e')
front = alist[:index]
behind = alist[index + 1:]
if '.' in behind or len(behind) == 0:
return False
isfront = self.isDigit(front)
isbehind = self.isDigit(behind)
return isfront and isbehind
else:
return self.isDigit(alist)
def isDigit(self, alist):
dotNum = 0
allow_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.']
for i in range(len(alist)):
if alist[i] not in allow_list:
return False
if alist[i] == '.':
dotNum += 1
if alist[i] in '+-' and i != 0:
return False
if dotNum > 1:
return False
return True
6、面试题21:调整数组的顺序使奇数位于偶数前面
- 题目描述:
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。
- 详细代码:
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
pBegin = 0
pEnd = len(array) - 1
while pBegin < pEnd:
while pBegin < pEnd and not self.isEven(array[pBegin]):
pBegin += 1
while pBegin < pEnd and self.isEven(array[pEnd]):
pEnd -= 1
if pBegin < pEnd:
temp = array[pBegin]
array[pBegin] = array[pEnd]
array[pEnd] = temp
return array
def isEven(self, number):
return number & 0x1 == 0
-
其他题目:在上题的基础上,要求奇数和奇数,偶数和偶数的相对位置保持不变。
-
详细代码:
#奇数和偶数的相对位置不变
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
if not array:
return array
res1 = []
res2 = []
for i in array:
if i & 0x1 != 0:
res1.append(i)
else:
res2.append(i)
return res1 + res2