数百道BAT等大厂最新Python面试真题,学到你手软!

24 阅读3分钟

所以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 道华为机试题。

请看示例:密码验证合格程序.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 道机试题。

请看示例:排序.py

# 冒泡排序

# 时间复杂度 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 算法题

这部分又包含三大块:

  • 二叉树

  • 栈和队列

  • 链表

在学习Python的过程中,往往因为没有资料或者没人指导从而导致自己不想学下去了,因此我特意准备了个群 592539176 ,群里有大量的PDF书籍、教程都给大家免费使用!不管是学习到哪个阶段的小伙伴都可以获取到自己相对应的资料!

我们来看一个示例:向有环的环形链表中插入新节点.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()

现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。

分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

了解详情:docs.qq.com/doc/DSnl3ZG…