寒假以来一直在投一些算法实习岗位,既然是算法,面大厂的话肯定少不了“手撕代码”的问题,怕自己忘掉,因此做已记录。目标是面试8个公司,现在已经面试了有三星、华为、快手、深信服、字节跳动,其中三星没有考代码题目。
三星
比较水,没有考代码问题。
华为
比较简单,就考察了一个斐波那契数列的问题。面试也是比较简单只考了一个算法题。
class Solution:
def fib(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
快手
目前已经面了两面了,结果还不清楚,两面总共考了4到代码题,只记得三道了
1、读取一个文件的图像,然后将结果作为数组输出。
import cv2
img_BGR = cv2.imread(path)
img = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
#返回的结果是numpy类型,转为list就可以了
print(img.tolist())
2、如何判断两个二叉树完全一致,节点的value也是一样的。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if not p and not q:
return True
if not p or not q:
return False
#递归
return p.val==q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
#迭代
res_p,res_q = [],[]
def helper(root,res):
queue = collections.deque()
queue.append(root)
while queue:
item = queue.popleft()
if item:
res.append(item.val)
queue.append(item.left)
queue.append(item.right)
else:
res.append('null')
helper(p,res_p)
helper(q,res_q)
return res_p==res_q
3、查找一个字符串中无重复字符的最长子串长度。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:
return 0
res = 1
for i in range(len(s)):
tmp = set()
tmp.add(s[i])
for j in range(i+1,len(s)):
if s[j] not in tmp:
tmp.add(s[j])
else:
break
res = max(res,len(tmp))
return res
k = -1
res = 0
c_dict = {}
for i, c in enumerate(s):
#如果出现重复的元素且下标是大于k
#更新k,更新c的下标
if c in c_dict and c_dict[c] > k:
k = c_dict[c]
c_dict[c] = i
#如果没有出现重复的元素,直接给字典添加并且计算更新res的长度
else:
c_dict[c] = i
res = max(res, i-k)
return res

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



