20

百道算法面试题集锦!Python 实现,含华为、BAT 等校招真题!

 5 years ago
source link: https://redstonewill.com/2134/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

春招临近,无论是要找工作的准毕业生,还是身在职场想要提升自己的程序员,提升自己的算法内功心法、提升 Python 编程能力,总是大有裨益的。今天,红色石头发现了一份好资源:Python 实现的面试题集锦!

这份资源名为:Interview-code-practice-python!包含了几百道算法面试题,而且全都使用 Python 编写了答案。有问有答,学得岂不快哉~

好了,话不多说,直接放上这份面试集锦的 GitHub 地址:

https://github.com/leeguandong/Interview-code-practice-python

这个项目资源总共包含了 5 个方面的真题,分别是:2017 校招真题、剑指 offer、华为机试、机试题、直通 BAT 算法题。

nIruAvn.png!web

接下来,我们分别来看一下具体内容。

1. 2017 校招真题

这部分包含了 37 道 2017 年的校招真题。

veYN3iz.png!web

每个题目都配备相应的 Python 实现。例如我们来看一个有趣的例子:餐厅.py

# 在 m 批客人中选择 n 批上 n 个桌子
n, m = [int(x) for x in input().split()]
# 每个桌子可容纳的最大人数
table = [int(x) for x in input().split()]
# 分别表示第 i 批客人的人数和预计消费金额
cus = [[int(x) for x in input().split()] for i in range(m)]

# 将桌子按可容纳人数从小到大排列
table.sort()
# 按每批顾客的人数从小到大排序
cus = sorted(cus, key=lambda x: x[0])
# 总金额
money = 0

for i in range(n):
# 盛放第 i 个可接受的顾客批 j
temp = []
# 注意 range 中要用 cus 的 length 时更新
for j in range(len(cus)):
if cus[j][0] > table[i]:
break
temp.append(cus[j])
# 按消费金额排序
temp = sorted(temp, key=lambda x: x[1])
if temp:
money += temp[-1][1]
# 此批客人已就坐
cus.remove()
print(money)

2. 剑指 offer

这部分共包含了 68 道剑指真题。

RFbAfmR.png!web

请看示例:变态青蛙跳.py

'''
题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法
'''

'''
因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
跳1级,剩下n-1级,则剩下跳法是f(n-1)
跳2级,剩下n-2级,则剩下跳法是f(n-2)
所以f(n)=f(n-1)+f(n-2)+...+f(1)
因为f(n-1)=f(n-2)+f(n-3)+...+f(1)
所以f(n)=2*f(n-1)
然后求解这个无穷级数的和,正确答案应该是:每次至少跳一个,至多跳n个,一共有f(n)=2n-1种跳法
29ms
5632k
'''

# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
return 2**(number-1)

3. 华为机试

这部分包含 41 道华为机试题。

MFr2InB.png!web

请看示例:密码验证合格程序.py

'''
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
'''

# import re, sys
#
# for i in sys.stdin.readlines():
# print("OK" if len(i.strip()) > 8 and sum(
# [1 if re.search(r"[A-Z]", i.strip()) else 0, 1 if re.search(r"[a-z]", i.strip()) else 0,
# 1 if re.search(r"[0-9]", i.strip()) else 0, 1 if re.search(r"[^0-9a-zA-Z]", i.strip()) else 0]) > 2 and sum(
#         map(lambda c: i.strip().count(i.strip()[c:c + 3]) > 1, range(1, len(i.strip()) - 3))) == 0 else "NG")

# 略微思考会发现,只需要判断长度为3的子串是否出现即可。因为假设子串长度为4的出现,则一定包括了长度为3的子串。同时需要注意,
# 本题说的子串指包括了部分子串重叠的情况,
# 例如Qw11111*ed这个是不能通过的。再就是需要注意,判断子串的时候只需要判断到len(str)-3就行了。

import sys

try:
# 大小写,字母,
def panchar(sss):
standard = [0] * 4
for i in sss:
# print(i)
# 0
# 2
# 1
# A
# b
# print(len(sss))
# 数字
if i.isdigit():
standard[0] = 1
# print(i.isdigit())
# 小写
if i.islower():
standard[1] = 1
# 大写
if i.isupper():
standard[2] = 1
# 全都是字母,数字,空格
if not i.isalpha() and not i.isdigit() and not i.isspace():
standard[3] = 1
if sum(standard) >= 3:
return False
return True

# 不能有相同长度超 2 的字串重复
def zichuan(sss):
for i in range(len(sss) - 3):
            zichuan_1 = sss[i: i + 3]
zichuan_2 = sss[i + 1::]
if zichuan_1 in zichuan_2:
return True
return False

result = []
while True:
line = sys.stdin.readline().strip()
if line == '':
break
if len(line) <= 8:
result.append('NG')
# 大小写字母.数字.其它符号
elif panchar(line):
result.append('NG')
elif zichuan(line):
result.append('NG')
else:
result.append('OK')
for i in result:
print(i)
except:
pass


# # 循环输入,try catch
# while True:
#     try:
# x = input().split()
#
#
#     except:
# pass

4. 机试题

这部分包含 3 道机试题。

JFJRZny.png!web

请看示例:排序.py

# https://blog.csdn.net/u012193416/article/details/78790448
# # 冒泡排序
# # 时间复杂度 O(n**2) 空间复杂度 O(1)
# x = [int(i) for i in input().split(',')]
#
# # print(x)
#
# def mpsort(x):
# n = len(x)
# # print(n)
# for i in range(n - 1):
# for j in range(0, n - i - 1):
# # print(x[j])
# if x[j] > x[j + 1]:
# x[j], x[j + 1] = x[j + 1], x[j]
# return x
#
# print(mpsort(x))

# # 选择排序
# # 时间复杂度 O(n**2) 空间复杂度 O(1)
# x = [int(i) for i in input().split(',')]
#
# def xzsort(x):
# n = len(x)
# for i in range(n - 1):
# min = i
# for j in range(i + 1, n):
# if x[j] < x[min]:
# min = j
# x[i], x[min] = x[min], x[i]
# return x
#
# print(xzsort(x))

# # 插入排序
# # 时间复杂度 O(n**2) 空间复杂度 O(1)
# x = [int(i) for i in input().split(',')]
#
# def crsort(x):
# n = len(x)
# for i in range(1, n):
# j = i
# while j > 0:
# if x[j] < x[j - 1]:
# x[j], x[j - 1] = x[j - 1], x[j]
# j -= 1
# else:
# break
# return x
#
# print(crsort(x))

# # 希尔排序
# # 时间复杂度 O(nlogn)-O(n**2) 空间复杂度 O(1)
# x = [int(i) for i in input().split(',')]
#
# def shellsort(x):
# n = len(x)
# gap = n // 2
#
# while gap > 0:
# for i in range(gap, n):
# j = i
# while j > 0:
# if x[j] < x[j - gap]:
# x[j], x[j - gap] = x[j - gap], x[j]
# j -= gap
# else:
# break
# gap //= 2
# return x
#
# print(shellsort(x))

# # 快速排序
# # 时间复杂度 O(nlogn) 空间复杂度 O(logn)-O(n)
# x = [int(i) for i in input().split(',')]
#
# def kpsort(x, first, last):
# font = first
# end = last
# middle = x[first]
#
# if first >= last:
# return
#
# while font < end:
# while font < end and x[font] <= middle:
# font += 1
# x[end] = x[font]
#
# while font < end and x[end] > middle:
# end -= 1
# x[font] = x[end]
#
# x[font] = middle
#
# kpsort(x, first, font - 1)
# kpsort(x, font + 1, last)

# 归并排序
# 时间复杂度 O(nlogn) 空间复杂度 O(N)
x = [int(i) for i in input().split(',')]

def gbsort(x):
length = len(x)
    if length <= 1:
return x
mid = length // 2

    left = gbsort(x[:mid])
    right = gbsort(x[mid:])

left_point, right_pointer = 0, 0
result = []

    while left_point < len(left) and right_pointer < len(right):
        if left[left_point] <= right[right_pointer]:
result.append(left[left_point])
left_point += 1
        else:
result.append(right_pointer)
right_pointer += 1

    result += left[left_point:]
result += right[right_pointer]

return result

print(gbsort(x))

5. 直通 BAT 算法题

这部分又包含三大块:

  • 二叉树
  • 栈和队列

  • 链表

我们来看一个示例:向有环的环形链表中插入新节点.py

# 指针给的是节点值
class Node():
def __init__(self, value=None):
self.value = value
self.next = None

def insertnum(head, num):
node = Node(num)
if head == None:
node.next = node
return node
node = head
pre = node
cur = node.next
while cur != head:
if pre.value > num and cur.value <= num:
break
pre = pre.next
cur = cur.next
# num 小于节点值,pre只跑到最后一个节点,node跑道头结点
pre.next = node
node.next = cur
# 是按顺序来的,返回的是 head 或者 node ,是有顺序决定的
return head if head.value < num else node

node = Node()
node.insertnum([[1, 2, 3], 5])

最后,一般学习 Python 的最好方法就是从底层算法开始实现一遍,过一遍基本函数与结构。有了充足的理解之后,就可以直接刷 LeetCode 等。希望这份 Python 面试题集锦对你有所帮助!

ymi2imU.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK