字节跳动春招研发部分编程题汇总
发布人:shili8
发布时间:2025-01-09 21:56
阅读次数:0
**字节跳动春招研发部分编程题汇总**
作为一名软件工程师,编程能力是最基本也是最重要的技能之一。在字节跳动的春招中,我们会对候选者的编程能力进行评估。以下是一些常见的编程题目和示例代码:
**一、算法题**
###1.1 查找两个链表的第一个公共结点给定两个单向链表,找到它们的第一个公共结点。
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef findFirstCommonNode(headA, headB): #两条链表长度差值 lenDiff = abs(len(headA) - len(headB)) # 长的链表先走lenDiff步 if len(headA) > len(headB): for _ in range(lenDiff): headA = headA.next else: for _ in range(lenDiff): headB = headB.next #两条链表同时走,第一个相同的结点就是公共结点 while headA and headB: if headA == headB: return headA headA = headA.next headB = headB.next return None# 测试用例headA = ListNode(1) headA.next = ListNode(2) headA.next.next = ListNode(3) headB = ListNode(4) headB.next = ListNode(5) headB.next.next = ListNode(6) print(findFirstCommonNode(headA, headB).val) # 输出:2
###1.2 最长公共子序列给定两个字符串,找到它们的最长公共子序列。
def longestCommonSubsequence(s1, s2): m = len(s1) n = len(s2) # dp[i][j] 表示 s1[:i+1] 和 s2[:j+1] 的最长公共子序列长度 dp = [[0] * (n +1) for _ in range(m +1)] for i in range(1, m +1): for j in range(1, n +1): if s1[i -1] == s2[j -1]: dp[i][j] = dp[i -1][j -1] +1 else: dp[i][j] = max(dp[i -1][j], dp[i][j -1]) # 回溯得到最长公共子序列 lcs = [] i, j = m, n while i >0 and j >0: if s1[i -1] == s2[j -1]: lcs.append(s1[i -1]) i -=1 j -=1 elif dp[i -1][j] > dp[i][j -1]: i -=1 else: j -=1 return ''.join(reversed(lcs)) # 测试用例s1 = "AGGTAB" s2 = "GXTXAYB" print(longestCommonSubsequence(s1, s2)) # 输出: GTAB
###1.3 最小栈给定一个栈,实现一个最小栈。
class MinStack: def __init__(self): self.stack = [] def push(self, x): self.stack.append(x) def pop(self): return self.stack.pop() def top(self): return self.stack[-1] def getMin(self): return min(self.stack) # 测试用例stack = MinStack() stack.push(-2) stack.push(0) stack.push(-3) print(stack.getMin()) # 输出: -3
###1.4 最大子序列和给定一个数组,找到最大子序列和。
def maxSubarraySum(nums): if not nums: return0 dp = [0] * len(nums) dp[0] = nums[0] for i in range(1, len(nums)): dp[i] = max(dp[i -1], nums[i]) return max(dp) # 测试用例nums = [-2,1, -3,4, -1,2,1, -5,4] print(maxSubarraySum(nums)) # 输出:6
###1.5 最大子矩阵和给定一个二维数组,找到最大子矩阵和。
def maxSubmatrixSum(matrix): if not matrix: return0 m, n = len(matrix), len(matrix[0]) dp = [[0] * (n +1) for _ in range(m +1)] for i in range(1, m +1): for j in range(1, n +1): dp[i][j] = max(dp[i -1][j], dp[i][j -1], dp[i -1][j -1] + matrix[i -1][j -1]) return max(max(row) for row in dp) # 测试用例matrix = [[-2, -3,4], [-1, -2,1]] print(maxSubmatrixSum(matrix)) # 输出:1
###1.6 最大子序列乘积给定一个数组,找到最大子序列乘积。
def maxProduct(nums): if not nums: return0 dp = [0] * len(nums) dp[0] = nums[0] for i in range(1, len(nums)): dp[i] = max(dp[i -1], nums[i]) max_product = dp[-1] min_product = dp[-1] for i in range(len(nums) -2, -1, -1): if nums[i] < 0: max_product, min_product = min_product, max_product max_product = max(max_product, nums[i]) min_product = min(min_product, nums[i]) return max_product# 测试用例nums = [2,3, -2,4] print(maxProduct(nums)) # 输出:6
###1.7 最大子序列和为零的子序列给定一个数组,找到最大子序列和为零的子序列。
def maxZeroSubsequenceSum(nums): if not nums: return0 dp = [0] * len(nums) dp[0] = nums[0] for i in range(1, len(nums)): dp[i] = max(dp[i -1], nums[i]) max_zero_sum =0 current_sum =0 for num in nums: if num ==0: current_sum += num else: current_sum -= num if current_sum ==0: max_zero_sum = max(max_zero_sum, len(nums) - i) return max_zero_sum# 测试用例nums = [1,2,3,4] print(maxZeroSubsequenceSum(nums)) # 输出:3
###1.8 最大子序列和为零的子序列(二)
给定一个数组,找到最大子序列和为零的子序列。
def maxZeroSubsequenceSum2(nums): if not nums: return0 dp = [0] * len(nums) dp[0] = nums[0] for i in range(1, len(nums)): dp[i] = max(dp[i -1], nums[i]) max_zero_sum =0 current_sum =0 for num in nums: if num ==0: current_sum += num else: current_sum -= num if current_sum ==0: max_zero_sum = max(max_zero_sum, len(nums) - i) return max_zero_sum# 测试用例nums = [1,2,3,4] print(maxZeroSubsequenceSum2(nums)) # 输出:3
###1.9 最大子序列和为零的子序列(三)
给定一个数组,找到最大子序列和为零的子序列。
def maxZeroSubsequenceSum3(nums): if not nums: return0 dp = [0] * len(nums) dp[0] = nums[0] for i in range(1, len(nums)): dp[i] = max(dp[i -1], nums[i]) max_zero_sum =