本文详细介绍了数据结构和算法的基础知识,并通过实例展示了数组、链表、栈、队列等常见数据结构的操作和应用场景。此外,文章还提供了数据结构和算法大厂面试真题的解析和解答,帮助读者更好地准备面试。
array[index]
# Python 示例代码 array = [1, 2, 3, 4, 5] # 数组的读取 print(array[2]) # 输出3 # 插入元素 array.insert(1, 10) print(array) # 输出[1, 10, 2, 3, 4, 5] # 删除元素 del array[1] print(array) # 输出[1, 2, 3, 4, 5] # 更新元素 array[1] = 20 print(array) # 输出[1, 20, 3, 4, 5]
# Python 示例代码 class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def insert(self, index, data): new_node = Node(data) if index == 0: new_node.next = self.head self.head = new_node return current = self.head for _ in range(index - 1): if current is None: raise IndexError("Index out of range") current = current.next new_node.next = current.next current.next = new_node def delete(self, index): if index == 0: self.head = self.head.next return current = self.head for _ in range(index - 1): if current is None: raise IndexError("Index out of range") current = current.next current.next = current.next.next
# Python 示例代码 class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): if not self.is_empty(): return self.items.pop() return None def peek(self): if not self.is_empty(): return self.items[-1] return None def is_empty(self): return len(self.items) == 0
# Python 示例代码 class Queue: def __init__(self): self.items = [] def enqueue(self, item): self.items.append(item) def dequeue(self): if not self.is_empty(): return self.items.pop(0) return None def peek(self): if not self.is_empty(): return self.items[0] return None def is_empty(self): return len(self.items) == 0
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] # 示例用例 arr = [64, 34, 25, 12, 22, 11, 90] bubble_sort(arr) print("Sorted array") print(arr)
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def insert(self, index, data): new_node = Node(data) if index == 0: new_node.next = self.head self.head = new_node return current = self.head for _ in range(index - 1): if current is None: raise IndexError("Index out of range") current = current.next new_node.next = current.next current.next = new_node def delete(self, index): if index == 0: self.head = self.head.next return current = self.head for _ in range(index - 1): if current is None: raise IndexError("Index out of range") current = current.next current.next = current.next.next # 示例用例 ll = LinkedList() ll.append(1) ll.append(2) ll.append(3) ll.insert(1, 4) print('链表:', ll.head.data, ll.head.next.data, ll.head.next.next.data, ll.head.next.next.next.data) ll.delete(1) print('删除后链表:', ll.head.data, ll.head.next.data, ll.head.next.next.data)
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): if not self.is_empty(): return self.items.pop() return None def peek(self): if not self.is_empty(): return self.items[-1] return None def is_empty(self): return len(self.items) == 0 # 示例用例 def is_balanced(s): stack = Stack() mapping = {")": "(", "}": "{", "]": "["} for char in s: if char in mapping: top = stack.pop() if stack else '#' if mapping[char] != top: return False else: stack.push(char) return stack.is_empty() print(is_balanced("{[]}()")) # 输出: True print(is_balanced("{[}]")) # 输出: False
class Queue: def __init__(self): self.items = [] def enqueue(self, item): self.items.append(item) def dequeue(self): if not self.is_empty(): return self.items.pop(0) return None def peek(self): if not self.is_empty(): return self.items[0] return None def is_empty(self): return len(self.items) == 0 # 示例用例 q = Queue() q.enqueue('任务1') q.enqueue('任务2') print('队列:', q.peek()) q.dequeue() print('任务1已执行') print('当前队列:', q.peek())
# Python 示例代码:DFS 遍历二叉树 class Node: def __init__(self, value): self.value = value self.left = None self.right = None def dfs_preorder(node): if node: print(node.value) dfs_preorder(node.left) dfs_preorder(node.right) # 示例用例 root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) dfs_preorder(root)
# Python 示例代码:BFS 遍历二叉树 from collections import deque def bfs(root): if root is None: return queue = deque([root]) while queue: node = queue.popleft() print(node.value) if node.left: queue.append(node.left) if node.right: queue.append(node.right) # 示例用例 root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) bfs(root)
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] # 示例用例 arr = [64, 34, 25, 12, 22, 11, 90] bubble_sort(arr) print("Sorted array") print(arr)
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 # 示例用例 arr = [2, 3, 4, 10, 40] target = 10 result = binary_search(arr, target) if result != -1: print("Element is present at index %d" % result) else: print("Element is not present in array")
def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key # 示例用例 arr = [12, 11, 13, 5, 6] insertion_sort(arr) print("Sorted array is:") print(arr)
class Node: def __init__(self, data): self.data = data self.next = None def reverse_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev # 示例用例 head = Node(1) second = Node(2) third = Node(3) head.next = second second.next = third new_head = reverse_list(head) while new_head: print(new_head.data, end=" ") new_head = new_head.next
def is_balanced(s): stack = [] mapping = {")": "(", "}": "{", "]": "["} for char in s: if char in mapping: top = stack.pop() if stack else '#' if mapping[char] != top: return False else: stack.append(char) return stack.is_empty() # 示例用例 print(is_balanced("{[]}()")) # 输出: True print(is_balanced("{[}]")) # 输出: False
def search_rotated_array(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid # 判断左半部分是否有序 if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 # 示例用例 nums = [4, 5, 6, 7, 0, 1, 2] target = 0 print(search_rotated_array(nums, target)) # 输出: 4
import heapq def find_largest_three(nums): min_heap = [] for num in nums: heapq.heappush(min_heap, num) if len(min_heap) > 3: heapq.heappop(min_heap) return sorted(min_heap, reverse=True) # 示例用例 nums = [10, 20, 30, 40, 50, 60, 70, 80] print(find_largest_three(nums)) # 输出: [60, 70, 80]
def longest_palindromic_substring(text): if not text: return "" n = len(text) longest = "" for i in range(n): # 奇数长度的回文子串 left, right = i, i while left >= 0 and right < n and text[left] == text[right]: if right - left + 1 > len(longest): longest = text[left:right + 1] left -= 1 right += 1 # 偶数长度的回文子串 left, right = i, i + 1 while left >= 0 and right < n and text[left] == text[right]: if right - left + 1 > len(longest): longest = text[left:right + 1] left -= 1 right += 1 return longest # 示例用例 text = "babad" print(longest_palindromic_substring(text)) # 输出: "bab"
基础知识复习
刷题练习
代码风格规范
沟通能力
代码逻辑
调试技巧
总结学习过程
高级数据结构
算法进阶
面试技巧
通过上述的回顾和解析,相信你已经能够更好地理解和应用数据结构与算法,并且在面试中能够从容应对各种挑战。继续努力,不断学习和实践,相信你会成为一名优秀的程序员。