分享秋招面试题
发布人:shili8
发布时间:2025-02-12 05:23
阅读次数:0
**秋招面试题汇总**
作为一名程序员,秋招是每年的一个重要阶段。为了帮助大家准备秋招面试,我将分享一些常见的面试题目,以及对应的解答和代码示例。
**一、算法与数据结构**
###1.1 数组#### 题目1:数组中找出第一个出现的重复数字* **描述**:在一个长度为 n 的数组里,所有数字都在0 到 n-1 之间范围内。现在这个数组是乱序的,我们想找出满足条件的第一个重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:2* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历数组,遇到重复数字时直接返回即可。
def findDuplicate(nums): count = {} for num in nums: if num in count: return num else: count[num] =1 return None# 测试用例nums = [2,3,5,4,8,6,7] print(findDuplicate(nums)) # 输出:2
#### 题目2:数组中找出第一个出现的不重复数字* **描述**:在一个长度为 n 的数组里,所有数字都在0 到 n-1 之间范围内。现在这个数组是乱序的,我们想找出满足条件的第一个不重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:5* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历数组,遇到不重复数字时直接返回即可。
def findFirstNotDuplicate(nums): count = {} for num in nums: if num in count: count[num] +=1 else: count[num] =1 for num in nums: if count[num] ==1: return num return None# 测试用例nums = [2,3,5,4,8,6,7] print(findFirstNotDuplicate(nums)) # 输出:5
###1.2 链表#### 题目1:链表中找出第一个出现的重复数字* **描述**:在一个长度为 n 的链表里,所有数字都在0 到 n-1 之间范围内。现在这个链表是乱序的,我们想找出满足条件的第一个重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:2* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历链表,遇到重复数字时直接返回即可。
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef findDuplicate(head): count = {} while head: if head.val in count: return head.val else: count[head.val] =1 head = head.next return None# 测试用例head = ListNode(2) head.next = ListNode(3) head.next.next = ListNode(5) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(8) head.next.next.next.next.next = ListNode(6) head.next.next.next.next.next.next = ListNode(7) print(findDuplicate(head)) # 输出:2
#### 题目2:链表中找出第一个出现的不重复数字* **描述**:在一个长度为 n 的链表里,所有数字都在0 到 n-1 之间范围内。现在这个链表是乱序的,我们想找出满足条件的第一个不重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:5* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历链表,遇到不重复数字时直接返回即可。
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef findFirstNotDuplicate(head): count = {} while head: if head.val in count: count[head.val] +=1 else: count[head.val] =1 head = head.next while head: if count[head.val] ==1: return head.val head = head.next return None# 测试用例head = ListNode(2) head.next = ListNode(3) head.next.next = ListNode(5) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(8) head.next.next.next.next.next = ListNode(6) head.next.next.next.next.next.next = ListNode(7) print(findFirstNotDuplicate(head)) # 输出:5
###1.3 栈与队列#### 题目1:栈中找出第一个出现的重复数字* **描述**:在一个长度为 n 的栈里,所有数字都在0 到 n-1 之间范围内。现在这个栈是乱序的,我们想找出满足条件的第一个重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:2* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历栈,遇到重复数字时直接返回即可。
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def findDuplicate(stack): count = {} while stack: num = stack.pop() if num in count: return num else: count[num] =1 return None# 测试用例stack = Stack() stack.push(2) stack.push(3) stack.push(5) stack.push(4) stack.push(8) stack.push(6) stack.push(7) print(findDuplicate(stack)) # 输出:2
#### 题目2:栈中找出第一个出现的不重复数字* **描述**:在一个长度为 n 的栈里,所有数字都在0 到 n-1 之间范围内。现在这个栈是乱序的,我们想找出满足条件的第一个不重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:5* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历栈,遇到不重复数字时直接返回即可。
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def findFirstNotDuplicate(stack): count = {} while stack: num = stack.pop() if num in count: count[num] +=1 else: count[num] =1 while stack: num = stack.pop() if count[num] ==1: return num return None# 测试用例stack = Stack() stack.push(2) stack.push(3) stack.push(5) stack.push(4) stack.push(8) stack.push(6) stack.push(7) print(findFirstNotDuplicate(stack)) # 输出:5
###1.4 队列#### 题目1:队列中找出第一个出现的重复数字* **描述**:在一个长度为 n 的队列里,所有数字都在0 到 n-1 之间范围内。现在这个队列是乱序的,我们想找出满足条件的第一个重复数字。
* **示例**:输入: [2,3,5,4,8,6,7] 输出:2* **解答**:首先我们可以使用哈希表来存储每个数字出现的次数。然后我们遍历队列,遇到重复数字时直接返回即可。
class Queue: def __init__(self): self.items = [] def enqueue(self, item): self.items.append(item)