Dataset Viewer
Auto-converted to Parquet Duplicate
problem_id
stringlengths
10
10
original_id
stringclasses
20 values
title
stringclasses
20 values
category
stringclasses
12 values
difficulty
stringclasses
3 values
companies
listlengths
2
2
description
stringclasses
20 values
solution_python
stringclasses
20 values
solution_java
stringclasses
20 values
solution_cpp
stringclasses
20 values
test_cases
listlengths
1
3
key_points
listlengths
2
2
tags
listlengths
4
4
algo_00001
two-sum
两数之和
array
easy
[ "Amazon", "Google" ]
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i return []
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) return new int[]{map.get(complement), i}; map.put(nums[i], i); } return new int[]...
vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; for (int i = 0; i < nums.size(); i++) { if (m.count(target - nums[i])) return {m[target - nums[i]], i}; m[nums[i]] = i; } return {}; }
[ "nums=[2,7,11,15], target=9 -> [0,1]", "nums=[3,2,4], target=6 -> [1,2]" ]
[ "哈希表一次遍历", "空间换时间" ]
[ "array", "easy", "Amazon", "Google" ]
algo_00002
merge-sorted
合并两个有序数组
array
easy
[ "Microsoft", "Uber" ]
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n,分别表示 nums1 和 nums2 中的元素数目。请你合并 nums2 到 nums1 中。
def merge(nums1, m, nums2, n): p1, p2, p = m-1, n-1, m+n-1 while p1 >= 0 and p2 >= 0: if nums1[p1] > nums2[p2]: nums1[p] = nums1[p1]; p1 -= 1 else: nums1[p] = nums2[p2]; p2 -= 1 p -= 1 while p2 >= 0: nums1[p] = nums2[p2]; p2 -= 1; p -= 1
public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) { nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; }
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; while (p2 >= 0) nums1[p--] = nums2[p2--]; }
[ "nums1=[1,2,3], m=3, nums2=[2,5,6], n=3 -> [1,2,2,3,5,6]", "nums1=[], m=0, nums2=[1], n=1 -> [1]" ]
[ "从后往前合并", "三指针技巧" ]
[ "array", "easy", "Microsoft", "Uber" ]
algo_00003
max-subarray
最大子数组和
array
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组,返回其最大和。
def maxSubArray(nums): max_sum = current = nums[0] for num in nums[1:]: current = max(num, current + num) max_sum = max(max_sum, current) return max_sum
public int maxSubArray(int[] nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); maxSum = Math.max(maxSum, curr); } return maxSum; }
int maxSubArray(vector<int>& nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.size(); i++) { curr = max(nums[i], curr + nums[i]); maxSum = max(maxSum, curr); } return maxSum; }
[ "nums=[-2,1,-3,4,-1,2,1,-5,4] -> 6", "nums=[1] -> 1" ]
[ "Kadane算法", "动态规划基础题" ]
[ "array", "medium", "Google", "Microsoft" ]
algo_00004
valid-parentheses
有效的括号
string
easy
[ "Google", "Uber" ]
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。
def isValid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in mapping: if not stack or stack[-1] != mapping[c]: return False stack.pop() else: stack.append(c) return not stack
public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> map = Map.of(')', '(', ']', '[', '}', '{'); for (char c : s.toCharArray()) { if (map.containsValue(c)) stack.push(c); else if (!stack.isEmpty() && stack.pop() != map.get(c)) return false; ...
bool isValid(string s) { stack<char> st; unordered_map<char, char> m = {{')','('},{']','['},{'}','{'}}; for (char c : s) { if (m.find(c) == m.end()) st.push(c); else if (st.empty() || st.top() != m[c]) return false; else st.pop(); } return st.empty(); }
[ "s=\"()\" -> true", "s=\"()[]{}\" -> true", "s=\"(]\" -> false" ]
[ "栈匹配法", "注意空栈边界" ]
[ "string", "easy", "Google", "Uber" ]
algo_00005
longest-palindrome
最长回文子串
string
medium
[ "Google", "Amazon" ]
给你一个字符串 s,找到 s 中最长的回文子串。
def longestPalindrome(s): def expand(l, r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 return l + 1, r - 1 start = end = 0 for i in range(len(s)): l1, r1 = expand(i, i) l2, r2 = expand(i, i + 1) if r1 - l1 > end - start: start, end = l1, r1 i...
public String longestPalindrome(String s) { int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len-1)/2; end = i + len/2; } } return s.substring...
string longestPalindrome(string s) { int start = 0, end = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); if (max(len1, len2) > end - start) { start = i - (max(len1,len2)-1)/2; end = i + max(len1,len2)/2; } } return s.substr(start, end - star...
[ "s=\"babad\" -> bab或aba", "s=\"cbbd\" -> bb" ]
[ "中心扩展法", "处理奇偶长度" ]
[ "string", "medium", "Google", "Amazon" ]
algo_00006
reverse-list
反转链表
linkedlist
medium
[ "Google", "Microsoft" ]
给你单链表的头节点 head,请你反转链表,并返回反转后的链表。
def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr, *curr = head; while (curr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; }
[ "head=[1,2,3,4,5] -> [5,4,3,2,1]", "head=[1,2] -> [2,1]" ]
[ "迭代法 O(1)空间", "递归法 O(n)空间" ]
[ "linkedlist", "medium", "Google", "Microsoft" ]
algo_00007
middle-node
链表的中间结点
linkedlist
easy
[ "Amazon", "Facebook" ]
给你单链表的头节点 head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
def middleNode(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
ListNode* middleNode(ListNode* head) { ListNode* slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow; }
[ "head=[1,2,3,4,5] -> 3", "head=[1,2,3,4,5,6] -> 4" ]
[ "快慢指针", "O(n)时间O(1)空间" ]
[ "linkedlist", "easy", "Amazon", "Facebook" ]
algo_00008
max-depth
二叉树的最大深度
tree
easy
[ "Microsoft", "Apple" ]
给定一个二叉树 root,返回其最大深度。
def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }
int maxDepth(TreeNode* root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }
[ "root=[3,9,20,null,null,15,7] -> 3", "root=[1,null,2] -> 2" ]
[ "DFS递归", "层序遍历也可" ]
[ "tree", "easy", "Microsoft", "Apple" ]
algo_00009
inorder-traversal
二叉树的中序遍历
tree
easy
[ "Google", "Microsoft" ]
给定一个二叉树的根节点 root,返回它的中序遍历结果。
def inorderTraversal(root): result = [] def dfs(node): if node: dfs(node.left) result.append(node.val) dfs(node.right) dfs(root) return result
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); dfs(root, res); return res; } private void dfs(TreeNode node, List<Integer> res) { if (node == null) return; dfs(node.left, res); res.add(node.val); dfs(node.right, res); }
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; dfs(root, res); return res; } void dfs(TreeNode* node, vector<int>& res) { if (!node) return; dfs(node->left, res); res.push_back(node->val); dfs(node->right, res); }
[ "root=[1,null,2,3] -> [1,3,2]", "root=[] -> []" ]
[ "左-根-右顺序", "递归最简洁" ]
[ "tree", "easy", "Google", "Microsoft" ]
algo_00010
binary-search
二分查找
binarysearch
easy
[ "Google", "Amazon" ]
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
[ "nums=[-1,0,3,5,9,12], target=9 -> 4", "nums=[-1,0,3,5,9,12], target=2 -> -1" ]
[ "标准模板", "注意防溢出写法" ]
[ "binarysearch", "easy", "Google", "Amazon" ]
algo_00011
climbing-stairs
爬楼梯
dp
easy
[ "Amazon", "Google" ]
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
def climbStairs(n): if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b
public int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
[ "n=2 -> 2", "n=3 -> 3", "n=5 -> 8" ]
[ "斐波那契数列", "滚动数组优化空间" ]
[ "dp", "easy", "Amazon", "Google" ]
algo_00012
coin-change
零钱兑换
dp
medium
[ "Google", "Amazon" ]
给你一个整数数组 coins 表示不同面额的硬币,以及一个整数 amount 表示总金额。计算并返回可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = Math.min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp...
int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp[amount]; }
[ "coins=[1,2,5], amount=11 -> 3", "coins=[2], amount=3 -> -1" ]
[ "完全背包问题", "dp[x] = min(dp[x-coin]+1)" ]
[ "dp", "medium", "Google", "Amazon" ]
algo_00013
subsets
子集
backtracking
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
def subsets(nums): result = [] def backtrack(start, path): result.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return result
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) { result.add(new ArrayList<>(path)); for (int...
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; backtrack(nums, 0, path, result); return result; } void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& result) { result.push_back(path); for (int i = start; i < nums.si...
[ "nums=[1,2,3] -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]" ]
[ "选或不选", "DFS遍历子集树" ]
[ "backtracking", "medium", "Google", "Microsoft" ]
algo_00014
permutations
全排列
backtracking
medium
[ "Google", "Facebook" ]
给定一个不含重复数字的数组 nums,返回其所有可能的全排列。
def permute(nums): result = [] def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack(path, used) ...
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] used = new boolean[nums.length]; backtrack(nums, used, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result...
vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<bool> used(nums.size(), false); vector<int> path; backtrack(nums, used, path, result); return result; } void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& result) { if (...
[ "nums=[1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]" ]
[ "标记已使用元素", "回溯经典模板" ]
[ "backtracking", "medium", "Google", "Facebook" ]
algo_00015
top-k-frequent
前K个高频元素
heap
medium
[ "Amazon", "Facebook" ]
给你一个整数数组 nums 和一个整数 k,请你返回其中出现频率前 k 高的元素。
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> count = new HashMap<>(); for (int n : nums) count.put(n, count.getOrDefault(n, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); for (Map.Entry<Integer, Inte...
vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> count; for (int n : nums) count[n]++; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap; for (auto& [num, freq] : count) { heap.push({freq, num}); if (heap.size() > k) heap.pop();...
[ "nums=[1,1,1,2,2,3], k=2 -> [1,2]", "nums=[1], k=1 -> [1]" ]
[ "小顶堆保持大小k", "时间复杂度O(nlogk)" ]
[ "heap", "medium", "Amazon", "Facebook" ]
algo_00016
3sum
三数之和
two-pointers
medium
[ "Google", "Amazon" ]
给你一个整数数组 nums,判断是否存在三元组满足和为0,返回所有不重复的三元组。
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif to...
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { ...
vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.size() - 1; while (left < right) { int tot...
[ "nums=[-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]" ]
[ "排序+双指针", "去重是关键" ]
[ "two-pointers", "medium", "Google", "Amazon" ]
algo_00017
container-water
盛最多水的容器
two-pointers
medium
[ "Google", "Uber" ]
给定一个长度为 n 的整数数组 height,找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
def maxArea(height): left, right = 0, len(height) - 1 max_area = 0 while left < right: h = min(height[left], height[right]) max_area = max(max_area, h * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return max_area
public int maxArea(int[] height) { int left = 0, right = height.length - 1, maxArea = 0; while (left < right) { int h = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return...
int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1, maxArea = 0; while (left < right) { int h = min(height[left], height[right]); maxArea = max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return maxArea; ...
[ "height=[1,8,6,2,5,4,8,3,7] -> 49" ]
[ "移动短边指针", "面积=min(h1,h2)*width" ]
[ "two-pointers", "medium", "Google", "Uber" ]
algo_00018
min-stack
最小栈
stack
medium
[ "Google", "Microsoft" ]
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val): self.stack.append(val) self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val)) def pop(self): self.stack.pop(); self.min_stack.pop() def top(self): ...
class MinStack { Stack<Integer> stack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); minStack.push(Math.min(val, minStack.isEmpty() ? val : minStack.peek())); } public void pop() { stack.pop(); minStack.pop(); } public int top() { return stack....
class MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(min(val, minSt.empty() ? val : minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
[ "push(-2), push(0), push(-3) -> getMin() returns -3" ]
[ "辅助栈记录最小值", "O(1)时间获取最小值" ]
[ "stack", "medium", "Google", "Microsoft" ]
algo_00019
lru-cache
LRU缓存
design
hard
[ "Google", "Amazon" ]
设计和实现一个 LRU(最近最少使用)缓存机制。
class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.order = [] def get(self, key): if key not in self.cache: return -1 self.order.remove(key); self.order.append(key) return self.cache[key] def put(self, key, value): ...
class LRUCache { int capacity; LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public LRUCache(int cap) { capacity = cap; } public int get(int key) { return cache.containsKey(key) ? cache.remove(key) : -1; } public void put(int key, int val) { if (cache.containsKey(key)) cache...
class LRUCache { int cap; list<pair<int,int>> dll; unordered_map<int, list<pair<int,int>>::iterator> cache; public: LRUCache(int capacity) : cap(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) return -1; dll.splice(dll.end(), dll, it->second)...
[ "put(1,1), put(2,2), get(1) -> 1" ]
[ "HashMap + 双向链表", "O(1)时间操作" ]
[ "design", "hard", "Google", "Amazon" ]
algo_00020
num-islands
岛屿数量
graph
medium
[ "Google", "Facebook" ]
给你一个由 '1'(陆地)和 '0'(水)组成的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,且只能由水平或垂直方向相邻的陆地连接形成。
def numIslands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) count = 0 def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1': return grid[r][c] = '0' dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1) for r in range(rows): ...
public int numIslands(char[][] grid) { if (grid.length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid[0].length; j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } private void dfs(char[][] grid, int r, int c) { ...
int numIslands(vector<vector<char>>& grid) { if (grid.empty()) return 0; int count = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } void dfs(vector<vector<char>>& grid, int r, int ...
[ "grid=[[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[0,0,0,0,0]] -> 1" ]
[ "DFS/BFS遍历", "访问后标记为0避免重复" ]
[ "graph", "medium", "Google", "Facebook" ]
algo_00021
two-sum
两数之和
array
easy
[ "Amazon", "Google" ]
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i return []
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) return new int[]{map.get(complement), i}; map.put(nums[i], i); } return new int[]...
vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; for (int i = 0; i < nums.size(); i++) { if (m.count(target - nums[i])) return {m[target - nums[i]], i}; m[nums[i]] = i; } return {}; }
[ "nums=[2,7,11,15], target=9 -> [0,1]", "nums=[3,2,4], target=6 -> [1,2]" ]
[ "哈希表一次遍历", "空间换时间" ]
[ "array", "easy", "Amazon", "Google" ]
algo_00022
merge-sorted
合并两个有序数组
array
easy
[ "Microsoft", "Uber" ]
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n,分别表示 nums1 和 nums2 中的元素数目。请你合并 nums2 到 nums1 中。
def merge(nums1, m, nums2, n): p1, p2, p = m-1, n-1, m+n-1 while p1 >= 0 and p2 >= 0: if nums1[p1] > nums2[p2]: nums1[p] = nums1[p1]; p1 -= 1 else: nums1[p] = nums2[p2]; p2 -= 1 p -= 1 while p2 >= 0: nums1[p] = nums2[p2]; p2 -= 1; p -= 1
public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) { nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; }
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; while (p2 >= 0) nums1[p--] = nums2[p2--]; }
[ "nums1=[1,2,3], m=3, nums2=[2,5,6], n=3 -> [1,2,2,3,5,6]", "nums1=[], m=0, nums2=[1], n=1 -> [1]" ]
[ "从后往前合并", "三指针技巧" ]
[ "array", "easy", "Microsoft", "Uber" ]
algo_00023
max-subarray
最大子数组和
array
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组,返回其最大和。
def maxSubArray(nums): max_sum = current = nums[0] for num in nums[1:]: current = max(num, current + num) max_sum = max(max_sum, current) return max_sum
public int maxSubArray(int[] nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); maxSum = Math.max(maxSum, curr); } return maxSum; }
int maxSubArray(vector<int>& nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.size(); i++) { curr = max(nums[i], curr + nums[i]); maxSum = max(maxSum, curr); } return maxSum; }
[ "nums=[-2,1,-3,4,-1,2,1,-5,4] -> 6", "nums=[1] -> 1" ]
[ "Kadane算法", "动态规划基础题" ]
[ "array", "medium", "Google", "Microsoft" ]
algo_00024
valid-parentheses
有效的括号
string
easy
[ "Google", "Uber" ]
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。
def isValid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in mapping: if not stack or stack[-1] != mapping[c]: return False stack.pop() else: stack.append(c) return not stack
public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> map = Map.of(')', '(', ']', '[', '}', '{'); for (char c : s.toCharArray()) { if (map.containsValue(c)) stack.push(c); else if (!stack.isEmpty() && stack.pop() != map.get(c)) return false; ...
bool isValid(string s) { stack<char> st; unordered_map<char, char> m = {{')','('},{']','['},{'}','{'}}; for (char c : s) { if (m.find(c) == m.end()) st.push(c); else if (st.empty() || st.top() != m[c]) return false; else st.pop(); } return st.empty(); }
[ "s=\"()\" -> true", "s=\"()[]{}\" -> true", "s=\"(]\" -> false" ]
[ "栈匹配法", "注意空栈边界" ]
[ "string", "easy", "Google", "Uber" ]
algo_00025
longest-palindrome
最长回文子串
string
medium
[ "Google", "Amazon" ]
给你一个字符串 s,找到 s 中最长的回文子串。
def longestPalindrome(s): def expand(l, r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 return l + 1, r - 1 start = end = 0 for i in range(len(s)): l1, r1 = expand(i, i) l2, r2 = expand(i, i + 1) if r1 - l1 > end - start: start, end = l1, r1 i...
public String longestPalindrome(String s) { int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len-1)/2; end = i + len/2; } } return s.substring...
string longestPalindrome(string s) { int start = 0, end = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); if (max(len1, len2) > end - start) { start = i - (max(len1,len2)-1)/2; end = i + max(len1,len2)/2; } } return s.substr(start, end - star...
[ "s=\"babad\" -> bab或aba", "s=\"cbbd\" -> bb" ]
[ "中心扩展法", "处理奇偶长度" ]
[ "string", "medium", "Google", "Amazon" ]
algo_00026
reverse-list
反转链表
linkedlist
medium
[ "Google", "Microsoft" ]
给你单链表的头节点 head,请你反转链表,并返回反转后的链表。
def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr, *curr = head; while (curr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; }
[ "head=[1,2,3,4,5] -> [5,4,3,2,1]", "head=[1,2] -> [2,1]" ]
[ "迭代法 O(1)空间", "递归法 O(n)空间" ]
[ "linkedlist", "medium", "Google", "Microsoft" ]
algo_00027
middle-node
链表的中间结点
linkedlist
easy
[ "Amazon", "Facebook" ]
给你单链表的头节点 head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
def middleNode(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
ListNode* middleNode(ListNode* head) { ListNode* slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow; }
[ "head=[1,2,3,4,5] -> 3", "head=[1,2,3,4,5,6] -> 4" ]
[ "快慢指针", "O(n)时间O(1)空间" ]
[ "linkedlist", "easy", "Amazon", "Facebook" ]
algo_00028
max-depth
二叉树的最大深度
tree
easy
[ "Microsoft", "Apple" ]
给定一个二叉树 root,返回其最大深度。
def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }
int maxDepth(TreeNode* root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }
[ "root=[3,9,20,null,null,15,7] -> 3", "root=[1,null,2] -> 2" ]
[ "DFS递归", "层序遍历也可" ]
[ "tree", "easy", "Microsoft", "Apple" ]
algo_00029
inorder-traversal
二叉树的中序遍历
tree
easy
[ "Google", "Microsoft" ]
给定一个二叉树的根节点 root,返回它的中序遍历结果。
def inorderTraversal(root): result = [] def dfs(node): if node: dfs(node.left) result.append(node.val) dfs(node.right) dfs(root) return result
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); dfs(root, res); return res; } private void dfs(TreeNode node, List<Integer> res) { if (node == null) return; dfs(node.left, res); res.add(node.val); dfs(node.right, res); }
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; dfs(root, res); return res; } void dfs(TreeNode* node, vector<int>& res) { if (!node) return; dfs(node->left, res); res.push_back(node->val); dfs(node->right, res); }
[ "root=[1,null,2,3] -> [1,3,2]", "root=[] -> []" ]
[ "左-根-右顺序", "递归最简洁" ]
[ "tree", "easy", "Google", "Microsoft" ]
algo_00030
binary-search
二分查找
binarysearch
easy
[ "Google", "Amazon" ]
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
[ "nums=[-1,0,3,5,9,12], target=9 -> 4", "nums=[-1,0,3,5,9,12], target=2 -> -1" ]
[ "标准模板", "注意防溢出写法" ]
[ "binarysearch", "easy", "Google", "Amazon" ]
algo_00031
climbing-stairs
爬楼梯
dp
easy
[ "Amazon", "Google" ]
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
def climbStairs(n): if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b
public int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
[ "n=2 -> 2", "n=3 -> 3", "n=5 -> 8" ]
[ "斐波那契数列", "滚动数组优化空间" ]
[ "dp", "easy", "Amazon", "Google" ]
algo_00032
coin-change
零钱兑换
dp
medium
[ "Google", "Amazon" ]
给你一个整数数组 coins 表示不同面额的硬币,以及一个整数 amount 表示总金额。计算并返回可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = Math.min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp...
int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp[amount]; }
[ "coins=[1,2,5], amount=11 -> 3", "coins=[2], amount=3 -> -1" ]
[ "完全背包问题", "dp[x] = min(dp[x-coin]+1)" ]
[ "dp", "medium", "Google", "Amazon" ]
algo_00033
subsets
子集
backtracking
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
def subsets(nums): result = [] def backtrack(start, path): result.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return result
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) { result.add(new ArrayList<>(path)); for (int...
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; backtrack(nums, 0, path, result); return result; } void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& result) { result.push_back(path); for (int i = start; i < nums.si...
[ "nums=[1,2,3] -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]" ]
[ "选或不选", "DFS遍历子集树" ]
[ "backtracking", "medium", "Google", "Microsoft" ]
algo_00034
permutations
全排列
backtracking
medium
[ "Google", "Facebook" ]
给定一个不含重复数字的数组 nums,返回其所有可能的全排列。
def permute(nums): result = [] def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack(path, used) ...
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] used = new boolean[nums.length]; backtrack(nums, used, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result...
vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<bool> used(nums.size(), false); vector<int> path; backtrack(nums, used, path, result); return result; } void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& result) { if (...
[ "nums=[1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]" ]
[ "标记已使用元素", "回溯经典模板" ]
[ "backtracking", "medium", "Google", "Facebook" ]
algo_00035
top-k-frequent
前K个高频元素
heap
medium
[ "Amazon", "Facebook" ]
给你一个整数数组 nums 和一个整数 k,请你返回其中出现频率前 k 高的元素。
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> count = new HashMap<>(); for (int n : nums) count.put(n, count.getOrDefault(n, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); for (Map.Entry<Integer, Inte...
vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> count; for (int n : nums) count[n]++; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap; for (auto& [num, freq] : count) { heap.push({freq, num}); if (heap.size() > k) heap.pop();...
[ "nums=[1,1,1,2,2,3], k=2 -> [1,2]", "nums=[1], k=1 -> [1]" ]
[ "小顶堆保持大小k", "时间复杂度O(nlogk)" ]
[ "heap", "medium", "Amazon", "Facebook" ]
algo_00036
3sum
三数之和
two-pointers
medium
[ "Google", "Amazon" ]
给你一个整数数组 nums,判断是否存在三元组满足和为0,返回所有不重复的三元组。
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif to...
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { ...
vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.size() - 1; while (left < right) { int tot...
[ "nums=[-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]" ]
[ "排序+双指针", "去重是关键" ]
[ "two-pointers", "medium", "Google", "Amazon" ]
algo_00037
container-water
盛最多水的容器
two-pointers
medium
[ "Google", "Uber" ]
给定一个长度为 n 的整数数组 height,找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
def maxArea(height): left, right = 0, len(height) - 1 max_area = 0 while left < right: h = min(height[left], height[right]) max_area = max(max_area, h * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return max_area
public int maxArea(int[] height) { int left = 0, right = height.length - 1, maxArea = 0; while (left < right) { int h = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return...
int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1, maxArea = 0; while (left < right) { int h = min(height[left], height[right]); maxArea = max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return maxArea; ...
[ "height=[1,8,6,2,5,4,8,3,7] -> 49" ]
[ "移动短边指针", "面积=min(h1,h2)*width" ]
[ "two-pointers", "medium", "Google", "Uber" ]
algo_00038
min-stack
最小栈
stack
medium
[ "Google", "Microsoft" ]
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val): self.stack.append(val) self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val)) def pop(self): self.stack.pop(); self.min_stack.pop() def top(self): ...
class MinStack { Stack<Integer> stack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); minStack.push(Math.min(val, minStack.isEmpty() ? val : minStack.peek())); } public void pop() { stack.pop(); minStack.pop(); } public int top() { return stack....
class MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(min(val, minSt.empty() ? val : minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
[ "push(-2), push(0), push(-3) -> getMin() returns -3" ]
[ "辅助栈记录最小值", "O(1)时间获取最小值" ]
[ "stack", "medium", "Google", "Microsoft" ]
algo_00039
lru-cache
LRU缓存
design
hard
[ "Google", "Amazon" ]
设计和实现一个 LRU(最近最少使用)缓存机制。
class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.order = [] def get(self, key): if key not in self.cache: return -1 self.order.remove(key); self.order.append(key) return self.cache[key] def put(self, key, value): ...
class LRUCache { int capacity; LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public LRUCache(int cap) { capacity = cap; } public int get(int key) { return cache.containsKey(key) ? cache.remove(key) : -1; } public void put(int key, int val) { if (cache.containsKey(key)) cache...
class LRUCache { int cap; list<pair<int,int>> dll; unordered_map<int, list<pair<int,int>>::iterator> cache; public: LRUCache(int capacity) : cap(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) return -1; dll.splice(dll.end(), dll, it->second)...
[ "put(1,1), put(2,2), get(1) -> 1" ]
[ "HashMap + 双向链表", "O(1)时间操作" ]
[ "design", "hard", "Google", "Amazon" ]
algo_00040
num-islands
岛屿数量
graph
medium
[ "Google", "Facebook" ]
给你一个由 '1'(陆地)和 '0'(水)组成的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,且只能由水平或垂直方向相邻的陆地连接形成。
def numIslands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) count = 0 def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1': return grid[r][c] = '0' dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1) for r in range(rows): ...
public int numIslands(char[][] grid) { if (grid.length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid[0].length; j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } private void dfs(char[][] grid, int r, int c) { ...
int numIslands(vector<vector<char>>& grid) { if (grid.empty()) return 0; int count = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } void dfs(vector<vector<char>>& grid, int r, int ...
[ "grid=[[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[0,0,0,0,0]] -> 1" ]
[ "DFS/BFS遍历", "访问后标记为0避免重复" ]
[ "graph", "medium", "Google", "Facebook" ]
algo_00041
two-sum
两数之和
array
easy
[ "Amazon", "Google" ]
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i return []
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) return new int[]{map.get(complement), i}; map.put(nums[i], i); } return new int[]...
vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; for (int i = 0; i < nums.size(); i++) { if (m.count(target - nums[i])) return {m[target - nums[i]], i}; m[nums[i]] = i; } return {}; }
[ "nums=[2,7,11,15], target=9 -> [0,1]", "nums=[3,2,4], target=6 -> [1,2]" ]
[ "哈希表一次遍历", "空间换时间" ]
[ "array", "easy", "Amazon", "Google" ]
algo_00042
merge-sorted
合并两个有序数组
array
easy
[ "Microsoft", "Uber" ]
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n,分别表示 nums1 和 nums2 中的元素数目。请你合并 nums2 到 nums1 中。
def merge(nums1, m, nums2, n): p1, p2, p = m-1, n-1, m+n-1 while p1 >= 0 and p2 >= 0: if nums1[p1] > nums2[p2]: nums1[p] = nums1[p1]; p1 -= 1 else: nums1[p] = nums2[p2]; p2 -= 1 p -= 1 while p2 >= 0: nums1[p] = nums2[p2]; p2 -= 1; p -= 1
public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) { nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; }
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; while (p2 >= 0) nums1[p--] = nums2[p2--]; }
[ "nums1=[1,2,3], m=3, nums2=[2,5,6], n=3 -> [1,2,2,3,5,6]", "nums1=[], m=0, nums2=[1], n=1 -> [1]" ]
[ "从后往前合并", "三指针技巧" ]
[ "array", "easy", "Microsoft", "Uber" ]
algo_00043
max-subarray
最大子数组和
array
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组,返回其最大和。
def maxSubArray(nums): max_sum = current = nums[0] for num in nums[1:]: current = max(num, current + num) max_sum = max(max_sum, current) return max_sum
public int maxSubArray(int[] nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); maxSum = Math.max(maxSum, curr); } return maxSum; }
int maxSubArray(vector<int>& nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.size(); i++) { curr = max(nums[i], curr + nums[i]); maxSum = max(maxSum, curr); } return maxSum; }
[ "nums=[-2,1,-3,4,-1,2,1,-5,4] -> 6", "nums=[1] -> 1" ]
[ "Kadane算法", "动态规划基础题" ]
[ "array", "medium", "Google", "Microsoft" ]
algo_00044
valid-parentheses
有效的括号
string
easy
[ "Google", "Uber" ]
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。
def isValid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in mapping: if not stack or stack[-1] != mapping[c]: return False stack.pop() else: stack.append(c) return not stack
public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> map = Map.of(')', '(', ']', '[', '}', '{'); for (char c : s.toCharArray()) { if (map.containsValue(c)) stack.push(c); else if (!stack.isEmpty() && stack.pop() != map.get(c)) return false; ...
bool isValid(string s) { stack<char> st; unordered_map<char, char> m = {{')','('},{']','['},{'}','{'}}; for (char c : s) { if (m.find(c) == m.end()) st.push(c); else if (st.empty() || st.top() != m[c]) return false; else st.pop(); } return st.empty(); }
[ "s=\"()\" -> true", "s=\"()[]{}\" -> true", "s=\"(]\" -> false" ]
[ "栈匹配法", "注意空栈边界" ]
[ "string", "easy", "Google", "Uber" ]
algo_00045
longest-palindrome
最长回文子串
string
medium
[ "Google", "Amazon" ]
给你一个字符串 s,找到 s 中最长的回文子串。
def longestPalindrome(s): def expand(l, r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 return l + 1, r - 1 start = end = 0 for i in range(len(s)): l1, r1 = expand(i, i) l2, r2 = expand(i, i + 1) if r1 - l1 > end - start: start, end = l1, r1 i...
public String longestPalindrome(String s) { int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len-1)/2; end = i + len/2; } } return s.substring...
string longestPalindrome(string s) { int start = 0, end = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); if (max(len1, len2) > end - start) { start = i - (max(len1,len2)-1)/2; end = i + max(len1,len2)/2; } } return s.substr(start, end - star...
[ "s=\"babad\" -> bab或aba", "s=\"cbbd\" -> bb" ]
[ "中心扩展法", "处理奇偶长度" ]
[ "string", "medium", "Google", "Amazon" ]
algo_00046
reverse-list
反转链表
linkedlist
medium
[ "Google", "Microsoft" ]
给你单链表的头节点 head,请你反转链表,并返回反转后的链表。
def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr, *curr = head; while (curr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; }
[ "head=[1,2,3,4,5] -> [5,4,3,2,1]", "head=[1,2] -> [2,1]" ]
[ "迭代法 O(1)空间", "递归法 O(n)空间" ]
[ "linkedlist", "medium", "Google", "Microsoft" ]
algo_00047
middle-node
链表的中间结点
linkedlist
easy
[ "Amazon", "Facebook" ]
给你单链表的头节点 head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
def middleNode(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
ListNode* middleNode(ListNode* head) { ListNode* slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow; }
[ "head=[1,2,3,4,5] -> 3", "head=[1,2,3,4,5,6] -> 4" ]
[ "快慢指针", "O(n)时间O(1)空间" ]
[ "linkedlist", "easy", "Amazon", "Facebook" ]
algo_00048
max-depth
二叉树的最大深度
tree
easy
[ "Microsoft", "Apple" ]
给定一个二叉树 root,返回其最大深度。
def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }
int maxDepth(TreeNode* root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }
[ "root=[3,9,20,null,null,15,7] -> 3", "root=[1,null,2] -> 2" ]
[ "DFS递归", "层序遍历也可" ]
[ "tree", "easy", "Microsoft", "Apple" ]
algo_00049
inorder-traversal
二叉树的中序遍历
tree
easy
[ "Google", "Microsoft" ]
给定一个二叉树的根节点 root,返回它的中序遍历结果。
def inorderTraversal(root): result = [] def dfs(node): if node: dfs(node.left) result.append(node.val) dfs(node.right) dfs(root) return result
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); dfs(root, res); return res; } private void dfs(TreeNode node, List<Integer> res) { if (node == null) return; dfs(node.left, res); res.add(node.val); dfs(node.right, res); }
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; dfs(root, res); return res; } void dfs(TreeNode* node, vector<int>& res) { if (!node) return; dfs(node->left, res); res.push_back(node->val); dfs(node->right, res); }
[ "root=[1,null,2,3] -> [1,3,2]", "root=[] -> []" ]
[ "左-根-右顺序", "递归最简洁" ]
[ "tree", "easy", "Google", "Microsoft" ]
algo_00050
binary-search
二分查找
binarysearch
easy
[ "Google", "Amazon" ]
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
[ "nums=[-1,0,3,5,9,12], target=9 -> 4", "nums=[-1,0,3,5,9,12], target=2 -> -1" ]
[ "标准模板", "注意防溢出写法" ]
[ "binarysearch", "easy", "Google", "Amazon" ]
algo_00051
climbing-stairs
爬楼梯
dp
easy
[ "Amazon", "Google" ]
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
def climbStairs(n): if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b
public int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
[ "n=2 -> 2", "n=3 -> 3", "n=5 -> 8" ]
[ "斐波那契数列", "滚动数组优化空间" ]
[ "dp", "easy", "Amazon", "Google" ]
algo_00052
coin-change
零钱兑换
dp
medium
[ "Google", "Amazon" ]
给你一个整数数组 coins 表示不同面额的硬币,以及一个整数 amount 表示总金额。计算并返回可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = Math.min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp...
int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp[amount]; }
[ "coins=[1,2,5], amount=11 -> 3", "coins=[2], amount=3 -> -1" ]
[ "完全背包问题", "dp[x] = min(dp[x-coin]+1)" ]
[ "dp", "medium", "Google", "Amazon" ]
algo_00053
subsets
子集
backtracking
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
def subsets(nums): result = [] def backtrack(start, path): result.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return result
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) { result.add(new ArrayList<>(path)); for (int...
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; backtrack(nums, 0, path, result); return result; } void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& result) { result.push_back(path); for (int i = start; i < nums.si...
[ "nums=[1,2,3] -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]" ]
[ "选或不选", "DFS遍历子集树" ]
[ "backtracking", "medium", "Google", "Microsoft" ]
algo_00054
permutations
全排列
backtracking
medium
[ "Google", "Facebook" ]
给定一个不含重复数字的数组 nums,返回其所有可能的全排列。
def permute(nums): result = [] def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack(path, used) ...
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] used = new boolean[nums.length]; backtrack(nums, used, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result...
vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<bool> used(nums.size(), false); vector<int> path; backtrack(nums, used, path, result); return result; } void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& result) { if (...
[ "nums=[1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]" ]
[ "标记已使用元素", "回溯经典模板" ]
[ "backtracking", "medium", "Google", "Facebook" ]
algo_00055
top-k-frequent
前K个高频元素
heap
medium
[ "Amazon", "Facebook" ]
给你一个整数数组 nums 和一个整数 k,请你返回其中出现频率前 k 高的元素。
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> count = new HashMap<>(); for (int n : nums) count.put(n, count.getOrDefault(n, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); for (Map.Entry<Integer, Inte...
vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> count; for (int n : nums) count[n]++; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap; for (auto& [num, freq] : count) { heap.push({freq, num}); if (heap.size() > k) heap.pop();...
[ "nums=[1,1,1,2,2,3], k=2 -> [1,2]", "nums=[1], k=1 -> [1]" ]
[ "小顶堆保持大小k", "时间复杂度O(nlogk)" ]
[ "heap", "medium", "Amazon", "Facebook" ]
algo_00056
3sum
三数之和
two-pointers
medium
[ "Google", "Amazon" ]
给你一个整数数组 nums,判断是否存在三元组满足和为0,返回所有不重复的三元组。
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif to...
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { ...
vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.size() - 1; while (left < right) { int tot...
[ "nums=[-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]" ]
[ "排序+双指针", "去重是关键" ]
[ "two-pointers", "medium", "Google", "Amazon" ]
algo_00057
container-water
盛最多水的容器
two-pointers
medium
[ "Google", "Uber" ]
给定一个长度为 n 的整数数组 height,找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
def maxArea(height): left, right = 0, len(height) - 1 max_area = 0 while left < right: h = min(height[left], height[right]) max_area = max(max_area, h * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return max_area
public int maxArea(int[] height) { int left = 0, right = height.length - 1, maxArea = 0; while (left < right) { int h = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return...
int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1, maxArea = 0; while (left < right) { int h = min(height[left], height[right]); maxArea = max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return maxArea; ...
[ "height=[1,8,6,2,5,4,8,3,7] -> 49" ]
[ "移动短边指针", "面积=min(h1,h2)*width" ]
[ "two-pointers", "medium", "Google", "Uber" ]
algo_00058
min-stack
最小栈
stack
medium
[ "Google", "Microsoft" ]
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val): self.stack.append(val) self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val)) def pop(self): self.stack.pop(); self.min_stack.pop() def top(self): ...
class MinStack { Stack<Integer> stack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); minStack.push(Math.min(val, minStack.isEmpty() ? val : minStack.peek())); } public void pop() { stack.pop(); minStack.pop(); } public int top() { return stack....
class MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(min(val, minSt.empty() ? val : minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
[ "push(-2), push(0), push(-3) -> getMin() returns -3" ]
[ "辅助栈记录最小值", "O(1)时间获取最小值" ]
[ "stack", "medium", "Google", "Microsoft" ]
algo_00059
lru-cache
LRU缓存
design
hard
[ "Google", "Amazon" ]
设计和实现一个 LRU(最近最少使用)缓存机制。
class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.order = [] def get(self, key): if key not in self.cache: return -1 self.order.remove(key); self.order.append(key) return self.cache[key] def put(self, key, value): ...
class LRUCache { int capacity; LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public LRUCache(int cap) { capacity = cap; } public int get(int key) { return cache.containsKey(key) ? cache.remove(key) : -1; } public void put(int key, int val) { if (cache.containsKey(key)) cache...
class LRUCache { int cap; list<pair<int,int>> dll; unordered_map<int, list<pair<int,int>>::iterator> cache; public: LRUCache(int capacity) : cap(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) return -1; dll.splice(dll.end(), dll, it->second)...
[ "put(1,1), put(2,2), get(1) -> 1" ]
[ "HashMap + 双向链表", "O(1)时间操作" ]
[ "design", "hard", "Google", "Amazon" ]
algo_00060
num-islands
岛屿数量
graph
medium
[ "Google", "Facebook" ]
给你一个由 '1'(陆地)和 '0'(水)组成的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,且只能由水平或垂直方向相邻的陆地连接形成。
def numIslands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) count = 0 def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1': return grid[r][c] = '0' dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1) for r in range(rows): ...
public int numIslands(char[][] grid) { if (grid.length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid[0].length; j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } private void dfs(char[][] grid, int r, int c) { ...
int numIslands(vector<vector<char>>& grid) { if (grid.empty()) return 0; int count = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } void dfs(vector<vector<char>>& grid, int r, int ...
[ "grid=[[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[0,0,0,0,0]] -> 1" ]
[ "DFS/BFS遍历", "访问后标记为0避免重复" ]
[ "graph", "medium", "Google", "Facebook" ]
algo_00061
two-sum
两数之和
array
easy
[ "Amazon", "Google" ]
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i return []
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) return new int[]{map.get(complement), i}; map.put(nums[i], i); } return new int[]...
vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; for (int i = 0; i < nums.size(); i++) { if (m.count(target - nums[i])) return {m[target - nums[i]], i}; m[nums[i]] = i; } return {}; }
[ "nums=[2,7,11,15], target=9 -> [0,1]", "nums=[3,2,4], target=6 -> [1,2]" ]
[ "哈希表一次遍历", "空间换时间" ]
[ "array", "easy", "Amazon", "Google" ]
algo_00062
merge-sorted
合并两个有序数组
array
easy
[ "Microsoft", "Uber" ]
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n,分别表示 nums1 和 nums2 中的元素数目。请你合并 nums2 到 nums1 中。
def merge(nums1, m, nums2, n): p1, p2, p = m-1, n-1, m+n-1 while p1 >= 0 and p2 >= 0: if nums1[p1] > nums2[p2]: nums1[p] = nums1[p1]; p1 -= 1 else: nums1[p] = nums2[p2]; p2 -= 1 p -= 1 while p2 >= 0: nums1[p] = nums2[p2]; p2 -= 1; p -= 1
public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) { nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; }
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; while (p2 >= 0) nums1[p--] = nums2[p2--]; }
[ "nums1=[1,2,3], m=3, nums2=[2,5,6], n=3 -> [1,2,2,3,5,6]", "nums1=[], m=0, nums2=[1], n=1 -> [1]" ]
[ "从后往前合并", "三指针技巧" ]
[ "array", "easy", "Microsoft", "Uber" ]
algo_00063
max-subarray
最大子数组和
array
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组,返回其最大和。
def maxSubArray(nums): max_sum = current = nums[0] for num in nums[1:]: current = max(num, current + num) max_sum = max(max_sum, current) return max_sum
public int maxSubArray(int[] nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); maxSum = Math.max(maxSum, curr); } return maxSum; }
int maxSubArray(vector<int>& nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.size(); i++) { curr = max(nums[i], curr + nums[i]); maxSum = max(maxSum, curr); } return maxSum; }
[ "nums=[-2,1,-3,4,-1,2,1,-5,4] -> 6", "nums=[1] -> 1" ]
[ "Kadane算法", "动态规划基础题" ]
[ "array", "medium", "Google", "Microsoft" ]
algo_00064
valid-parentheses
有效的括号
string
easy
[ "Google", "Uber" ]
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。
def isValid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in mapping: if not stack or stack[-1] != mapping[c]: return False stack.pop() else: stack.append(c) return not stack
public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> map = Map.of(')', '(', ']', '[', '}', '{'); for (char c : s.toCharArray()) { if (map.containsValue(c)) stack.push(c); else if (!stack.isEmpty() && stack.pop() != map.get(c)) return false; ...
bool isValid(string s) { stack<char> st; unordered_map<char, char> m = {{')','('},{']','['},{'}','{'}}; for (char c : s) { if (m.find(c) == m.end()) st.push(c); else if (st.empty() || st.top() != m[c]) return false; else st.pop(); } return st.empty(); }
[ "s=\"()\" -> true", "s=\"()[]{}\" -> true", "s=\"(]\" -> false" ]
[ "栈匹配法", "注意空栈边界" ]
[ "string", "easy", "Google", "Uber" ]
algo_00065
longest-palindrome
最长回文子串
string
medium
[ "Google", "Amazon" ]
给你一个字符串 s,找到 s 中最长的回文子串。
def longestPalindrome(s): def expand(l, r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 return l + 1, r - 1 start = end = 0 for i in range(len(s)): l1, r1 = expand(i, i) l2, r2 = expand(i, i + 1) if r1 - l1 > end - start: start, end = l1, r1 i...
public String longestPalindrome(String s) { int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len-1)/2; end = i + len/2; } } return s.substring...
string longestPalindrome(string s) { int start = 0, end = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); if (max(len1, len2) > end - start) { start = i - (max(len1,len2)-1)/2; end = i + max(len1,len2)/2; } } return s.substr(start, end - star...
[ "s=\"babad\" -> bab或aba", "s=\"cbbd\" -> bb" ]
[ "中心扩展法", "处理奇偶长度" ]
[ "string", "medium", "Google", "Amazon" ]
algo_00066
reverse-list
反转链表
linkedlist
medium
[ "Google", "Microsoft" ]
给你单链表的头节点 head,请你反转链表,并返回反转后的链表。
def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr, *curr = head; while (curr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; }
[ "head=[1,2,3,4,5] -> [5,4,3,2,1]", "head=[1,2] -> [2,1]" ]
[ "迭代法 O(1)空间", "递归法 O(n)空间" ]
[ "linkedlist", "medium", "Google", "Microsoft" ]
algo_00067
middle-node
链表的中间结点
linkedlist
easy
[ "Amazon", "Facebook" ]
给你单链表的头节点 head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
def middleNode(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
ListNode* middleNode(ListNode* head) { ListNode* slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow; }
[ "head=[1,2,3,4,5] -> 3", "head=[1,2,3,4,5,6] -> 4" ]
[ "快慢指针", "O(n)时间O(1)空间" ]
[ "linkedlist", "easy", "Amazon", "Facebook" ]
algo_00068
max-depth
二叉树的最大深度
tree
easy
[ "Microsoft", "Apple" ]
给定一个二叉树 root,返回其最大深度。
def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }
int maxDepth(TreeNode* root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }
[ "root=[3,9,20,null,null,15,7] -> 3", "root=[1,null,2] -> 2" ]
[ "DFS递归", "层序遍历也可" ]
[ "tree", "easy", "Microsoft", "Apple" ]
algo_00069
inorder-traversal
二叉树的中序遍历
tree
easy
[ "Google", "Microsoft" ]
给定一个二叉树的根节点 root,返回它的中序遍历结果。
def inorderTraversal(root): result = [] def dfs(node): if node: dfs(node.left) result.append(node.val) dfs(node.right) dfs(root) return result
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); dfs(root, res); return res; } private void dfs(TreeNode node, List<Integer> res) { if (node == null) return; dfs(node.left, res); res.add(node.val); dfs(node.right, res); }
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; dfs(root, res); return res; } void dfs(TreeNode* node, vector<int>& res) { if (!node) return; dfs(node->left, res); res.push_back(node->val); dfs(node->right, res); }
[ "root=[1,null,2,3] -> [1,3,2]", "root=[] -> []" ]
[ "左-根-右顺序", "递归最简洁" ]
[ "tree", "easy", "Google", "Microsoft" ]
algo_00070
binary-search
二分查找
binarysearch
easy
[ "Google", "Amazon" ]
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
[ "nums=[-1,0,3,5,9,12], target=9 -> 4", "nums=[-1,0,3,5,9,12], target=2 -> -1" ]
[ "标准模板", "注意防溢出写法" ]
[ "binarysearch", "easy", "Google", "Amazon" ]
algo_00071
climbing-stairs
爬楼梯
dp
easy
[ "Amazon", "Google" ]
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
def climbStairs(n): if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b
public int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
[ "n=2 -> 2", "n=3 -> 3", "n=5 -> 8" ]
[ "斐波那契数列", "滚动数组优化空间" ]
[ "dp", "easy", "Amazon", "Google" ]
algo_00072
coin-change
零钱兑换
dp
medium
[ "Google", "Amazon" ]
给你一个整数数组 coins 表示不同面额的硬币,以及一个整数 amount 表示总金额。计算并返回可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = Math.min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp...
int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp[amount]; }
[ "coins=[1,2,5], amount=11 -> 3", "coins=[2], amount=3 -> -1" ]
[ "完全背包问题", "dp[x] = min(dp[x-coin]+1)" ]
[ "dp", "medium", "Google", "Amazon" ]
algo_00073
subsets
子集
backtracking
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
def subsets(nums): result = [] def backtrack(start, path): result.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return result
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) { result.add(new ArrayList<>(path)); for (int...
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; backtrack(nums, 0, path, result); return result; } void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& result) { result.push_back(path); for (int i = start; i < nums.si...
[ "nums=[1,2,3] -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]" ]
[ "选或不选", "DFS遍历子集树" ]
[ "backtracking", "medium", "Google", "Microsoft" ]
algo_00074
permutations
全排列
backtracking
medium
[ "Google", "Facebook" ]
给定一个不含重复数字的数组 nums,返回其所有可能的全排列。
def permute(nums): result = [] def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack(path, used) ...
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] used = new boolean[nums.length]; backtrack(nums, used, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result...
vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<bool> used(nums.size(), false); vector<int> path; backtrack(nums, used, path, result); return result; } void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& result) { if (...
[ "nums=[1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]" ]
[ "标记已使用元素", "回溯经典模板" ]
[ "backtracking", "medium", "Google", "Facebook" ]
algo_00075
top-k-frequent
前K个高频元素
heap
medium
[ "Amazon", "Facebook" ]
给你一个整数数组 nums 和一个整数 k,请你返回其中出现频率前 k 高的元素。
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> count = new HashMap<>(); for (int n : nums) count.put(n, count.getOrDefault(n, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); for (Map.Entry<Integer, Inte...
vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> count; for (int n : nums) count[n]++; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap; for (auto& [num, freq] : count) { heap.push({freq, num}); if (heap.size() > k) heap.pop();...
[ "nums=[1,1,1,2,2,3], k=2 -> [1,2]", "nums=[1], k=1 -> [1]" ]
[ "小顶堆保持大小k", "时间复杂度O(nlogk)" ]
[ "heap", "medium", "Amazon", "Facebook" ]
algo_00076
3sum
三数之和
two-pointers
medium
[ "Google", "Amazon" ]
给你一个整数数组 nums,判断是否存在三元组满足和为0,返回所有不重复的三元组。
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif to...
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { ...
vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.size() - 1; while (left < right) { int tot...
[ "nums=[-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]" ]
[ "排序+双指针", "去重是关键" ]
[ "two-pointers", "medium", "Google", "Amazon" ]
algo_00077
container-water
盛最多水的容器
two-pointers
medium
[ "Google", "Uber" ]
给定一个长度为 n 的整数数组 height,找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
def maxArea(height): left, right = 0, len(height) - 1 max_area = 0 while left < right: h = min(height[left], height[right]) max_area = max(max_area, h * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return max_area
public int maxArea(int[] height) { int left = 0, right = height.length - 1, maxArea = 0; while (left < right) { int h = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return...
int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1, maxArea = 0; while (left < right) { int h = min(height[left], height[right]); maxArea = max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return maxArea; ...
[ "height=[1,8,6,2,5,4,8,3,7] -> 49" ]
[ "移动短边指针", "面积=min(h1,h2)*width" ]
[ "two-pointers", "medium", "Google", "Uber" ]
algo_00078
min-stack
最小栈
stack
medium
[ "Google", "Microsoft" ]
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val): self.stack.append(val) self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val)) def pop(self): self.stack.pop(); self.min_stack.pop() def top(self): ...
class MinStack { Stack<Integer> stack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); minStack.push(Math.min(val, minStack.isEmpty() ? val : minStack.peek())); } public void pop() { stack.pop(); minStack.pop(); } public int top() { return stack....
class MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(min(val, minSt.empty() ? val : minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
[ "push(-2), push(0), push(-3) -> getMin() returns -3" ]
[ "辅助栈记录最小值", "O(1)时间获取最小值" ]
[ "stack", "medium", "Google", "Microsoft" ]
algo_00079
lru-cache
LRU缓存
design
hard
[ "Google", "Amazon" ]
设计和实现一个 LRU(最近最少使用)缓存机制。
class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.order = [] def get(self, key): if key not in self.cache: return -1 self.order.remove(key); self.order.append(key) return self.cache[key] def put(self, key, value): ...
class LRUCache { int capacity; LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public LRUCache(int cap) { capacity = cap; } public int get(int key) { return cache.containsKey(key) ? cache.remove(key) : -1; } public void put(int key, int val) { if (cache.containsKey(key)) cache...
class LRUCache { int cap; list<pair<int,int>> dll; unordered_map<int, list<pair<int,int>>::iterator> cache; public: LRUCache(int capacity) : cap(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) return -1; dll.splice(dll.end(), dll, it->second)...
[ "put(1,1), put(2,2), get(1) -> 1" ]
[ "HashMap + 双向链表", "O(1)时间操作" ]
[ "design", "hard", "Google", "Amazon" ]
algo_00080
num-islands
岛屿数量
graph
medium
[ "Google", "Facebook" ]
给你一个由 '1'(陆地)和 '0'(水)组成的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,且只能由水平或垂直方向相邻的陆地连接形成。
def numIslands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) count = 0 def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1': return grid[r][c] = '0' dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1) for r in range(rows): ...
public int numIslands(char[][] grid) { if (grid.length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid[0].length; j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } private void dfs(char[][] grid, int r, int c) { ...
int numIslands(vector<vector<char>>& grid) { if (grid.empty()) return 0; int count = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } void dfs(vector<vector<char>>& grid, int r, int ...
[ "grid=[[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[0,0,0,0,0]] -> 1" ]
[ "DFS/BFS遍历", "访问后标记为0避免重复" ]
[ "graph", "medium", "Google", "Facebook" ]
algo_00081
two-sum
两数之和
array
easy
[ "Amazon", "Google" ]
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i return []
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) return new int[]{map.get(complement), i}; map.put(nums[i], i); } return new int[]...
vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; for (int i = 0; i < nums.size(); i++) { if (m.count(target - nums[i])) return {m[target - nums[i]], i}; m[nums[i]] = i; } return {}; }
[ "nums=[2,7,11,15], target=9 -> [0,1]", "nums=[3,2,4], target=6 -> [1,2]" ]
[ "哈希表一次遍历", "空间换时间" ]
[ "array", "easy", "Amazon", "Google" ]
algo_00082
merge-sorted
合并两个有序数组
array
easy
[ "Microsoft", "Uber" ]
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n,分别表示 nums1 和 nums2 中的元素数目。请你合并 nums2 到 nums1 中。
def merge(nums1, m, nums2, n): p1, p2, p = m-1, n-1, m+n-1 while p1 >= 0 and p2 >= 0: if nums1[p1] > nums2[p2]: nums1[p] = nums1[p1]; p1 -= 1 else: nums1[p] = nums2[p2]; p2 -= 1 p -= 1 while p2 >= 0: nums1[p] = nums2[p2]; p2 -= 1; p -= 1
public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) { nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; }
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p1 = m-1, p2 = n-1, p = m+n-1; while (p1 >= 0 && p2 >= 0) nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; while (p2 >= 0) nums1[p--] = nums2[p2--]; }
[ "nums1=[1,2,3], m=3, nums2=[2,5,6], n=3 -> [1,2,2,3,5,6]", "nums1=[], m=0, nums2=[1], n=1 -> [1]" ]
[ "从后往前合并", "三指针技巧" ]
[ "array", "easy", "Microsoft", "Uber" ]
algo_00083
max-subarray
最大子数组和
array
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组,返回其最大和。
def maxSubArray(nums): max_sum = current = nums[0] for num in nums[1:]: current = max(num, current + num) max_sum = max(max_sum, current) return max_sum
public int maxSubArray(int[] nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); maxSum = Math.max(maxSum, curr); } return maxSum; }
int maxSubArray(vector<int>& nums) { int maxSum = nums[0], curr = nums[0]; for (int i = 1; i < nums.size(); i++) { curr = max(nums[i], curr + nums[i]); maxSum = max(maxSum, curr); } return maxSum; }
[ "nums=[-2,1,-3,4,-1,2,1,-5,4] -> 6", "nums=[1] -> 1" ]
[ "Kadane算法", "动态规划基础题" ]
[ "array", "medium", "Google", "Microsoft" ]
algo_00084
valid-parentheses
有效的括号
string
easy
[ "Google", "Uber" ]
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。
def isValid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in mapping: if not stack or stack[-1] != mapping[c]: return False stack.pop() else: stack.append(c) return not stack
public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> map = Map.of(')', '(', ']', '[', '}', '{'); for (char c : s.toCharArray()) { if (map.containsValue(c)) stack.push(c); else if (!stack.isEmpty() && stack.pop() != map.get(c)) return false; ...
bool isValid(string s) { stack<char> st; unordered_map<char, char> m = {{')','('},{']','['},{'}','{'}}; for (char c : s) { if (m.find(c) == m.end()) st.push(c); else if (st.empty() || st.top() != m[c]) return false; else st.pop(); } return st.empty(); }
[ "s=\"()\" -> true", "s=\"()[]{}\" -> true", "s=\"(]\" -> false" ]
[ "栈匹配法", "注意空栈边界" ]
[ "string", "easy", "Google", "Uber" ]
algo_00085
longest-palindrome
最长回文子串
string
medium
[ "Google", "Amazon" ]
给你一个字符串 s,找到 s 中最长的回文子串。
def longestPalindrome(s): def expand(l, r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1; r += 1 return l + 1, r - 1 start = end = 0 for i in range(len(s)): l1, r1 = expand(i, i) l2, r2 = expand(i, i + 1) if r1 - l1 > end - start: start, end = l1, r1 i...
public String longestPalindrome(String s) { int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len-1)/2; end = i + len/2; } } return s.substring...
string longestPalindrome(string s) { int start = 0, end = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i), len2 = expand(s, i, i+1); if (max(len1, len2) > end - start) { start = i - (max(len1,len2)-1)/2; end = i + max(len1,len2)/2; } } return s.substr(start, end - star...
[ "s=\"babad\" -> bab或aba", "s=\"cbbd\" -> bb" ]
[ "中心扩展法", "处理奇偶长度" ]
[ "string", "medium", "Google", "Amazon" ]
algo_00086
reverse-list
反转链表
linkedlist
medium
[ "Google", "Microsoft" ]
给你单链表的头节点 head,请你反转链表,并返回反转后的链表。
def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr, *curr = head; while (curr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; }
[ "head=[1,2,3,4,5] -> [5,4,3,2,1]", "head=[1,2] -> [2,1]" ]
[ "迭代法 O(1)空间", "递归法 O(n)空间" ]
[ "linkedlist", "medium", "Google", "Microsoft" ]
algo_00087
middle-node
链表的中间结点
linkedlist
easy
[ "Amazon", "Facebook" ]
给你单链表的头节点 head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
def middleNode(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
ListNode* middleNode(ListNode* head) { ListNode* slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow; }
[ "head=[1,2,3,4,5] -> 3", "head=[1,2,3,4,5,6] -> 4" ]
[ "快慢指针", "O(n)时间O(1)空间" ]
[ "linkedlist", "easy", "Amazon", "Facebook" ]
algo_00088
max-depth
二叉树的最大深度
tree
easy
[ "Microsoft", "Apple" ]
给定一个二叉树 root,返回其最大深度。
def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }
int maxDepth(TreeNode* root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }
[ "root=[3,9,20,null,null,15,7] -> 3", "root=[1,null,2] -> 2" ]
[ "DFS递归", "层序遍历也可" ]
[ "tree", "easy", "Microsoft", "Apple" ]
algo_00089
inorder-traversal
二叉树的中序遍历
tree
easy
[ "Google", "Microsoft" ]
给定一个二叉树的根节点 root,返回它的中序遍历结果。
def inorderTraversal(root): result = [] def dfs(node): if node: dfs(node.left) result.append(node.val) dfs(node.right) dfs(root) return result
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); dfs(root, res); return res; } private void dfs(TreeNode node, List<Integer> res) { if (node == null) return; dfs(node.left, res); res.add(node.val); dfs(node.right, res); }
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; dfs(root, res); return res; } void dfs(TreeNode* node, vector<int>& res) { if (!node) return; dfs(node->left, res); res.push_back(node->val); dfs(node->right, res); }
[ "root=[1,null,2,3] -> [1,3,2]", "root=[] -> []" ]
[ "左-根-右顺序", "递归最简洁" ]
[ "tree", "easy", "Google", "Microsoft" ]
algo_00090
binary-search
二分查找
binarysearch
easy
[ "Google", "Amazon" ]
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
[ "nums=[-1,0,3,5,9,12], target=9 -> 4", "nums=[-1,0,3,5,9,12], target=2 -> -1" ]
[ "标准模板", "注意防溢出写法" ]
[ "binarysearch", "easy", "Google", "Amazon" ]
algo_00091
climbing-stairs
爬楼梯
dp
easy
[ "Amazon", "Google" ]
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
def climbStairs(n): if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b
public int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
int climbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; }
[ "n=2 -> 2", "n=3 -> 3", "n=5 -> 8" ]
[ "斐波那契数列", "滚动数组优化空间" ]
[ "dp", "easy", "Amazon", "Google" ]
algo_00092
coin-change
零钱兑换
dp
medium
[ "Google", "Amazon" ]
给你一个整数数组 coins 表示不同面额的硬币,以及一个整数 amount 表示总金额。计算并返回可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = Math.min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp...
int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int coin : coins) { for (int x = coin; x <= amount; x++) { dp[x] = min(dp[x], dp[x - coin] + 1); } } return dp[amount] > amount ? -1 : dp[amount]; }
[ "coins=[1,2,5], amount=11 -> 3", "coins=[2], amount=3 -> -1" ]
[ "完全背包问题", "dp[x] = min(dp[x-coin]+1)" ]
[ "dp", "medium", "Google", "Amazon" ]
algo_00093
subsets
子集
backtracking
medium
[ "Google", "Microsoft" ]
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
def subsets(nums): result = [] def backtrack(start, path): result.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return result
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) { result.add(new ArrayList<>(path)); for (int...
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; backtrack(nums, 0, path, result); return result; } void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& result) { result.push_back(path); for (int i = start; i < nums.si...
[ "nums=[1,2,3] -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]" ]
[ "选或不选", "DFS遍历子集树" ]
[ "backtracking", "medium", "Google", "Microsoft" ]
algo_00094
permutations
全排列
backtracking
medium
[ "Google", "Facebook" ]
给定一个不含重复数字的数组 nums,返回其所有可能的全排列。
def permute(nums): result = [] def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack(path, used) ...
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] used = new boolean[nums.length]; backtrack(nums, used, new ArrayList<>(), result); return result; } private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result...
vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<bool> used(nums.size(), false); vector<int> path; backtrack(nums, used, path, result); return result; } void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& result) { if (...
[ "nums=[1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]" ]
[ "标记已使用元素", "回溯经典模板" ]
[ "backtracking", "medium", "Google", "Facebook" ]
algo_00095
top-k-frequent
前K个高频元素
heap
medium
[ "Amazon", "Facebook" ]
给你一个整数数组 nums 和一个整数 k,请你返回其中出现频率前 k 高的元素。
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> count = new HashMap<>(); for (int n : nums) count.put(n, count.getOrDefault(n, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); for (Map.Entry<Integer, Inte...
vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> count; for (int n : nums) count[n]++; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap; for (auto& [num, freq] : count) { heap.push({freq, num}); if (heap.size() > k) heap.pop();...
[ "nums=[1,1,1,2,2,3], k=2 -> [1,2]", "nums=[1], k=1 -> [1]" ]
[ "小顶堆保持大小k", "时间复杂度O(nlogk)" ]
[ "heap", "medium", "Amazon", "Facebook" ]
algo_00096
3sum
三数之和
two-pointers
medium
[ "Google", "Amazon" ]
给你一个整数数组 nums,判断是否存在三元组满足和为0,返回所有不重复的三元组。
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif to...
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { ...
vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = nums.size() - 1; while (left < right) { int tot...
[ "nums=[-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]" ]
[ "排序+双指针", "去重是关键" ]
[ "two-pointers", "medium", "Google", "Amazon" ]
algo_00097
container-water
盛最多水的容器
two-pointers
medium
[ "Google", "Uber" ]
给定一个长度为 n 的整数数组 height,找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
def maxArea(height): left, right = 0, len(height) - 1 max_area = 0 while left < right: h = min(height[left], height[right]) max_area = max(max_area, h * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return max_area
public int maxArea(int[] height) { int left = 0, right = height.length - 1, maxArea = 0; while (left < right) { int h = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return...
int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1, maxArea = 0; while (left < right) { int h = min(height[left], height[right]); maxArea = max(maxArea, h * (right - left)); if (height[left] < height[right]) left++; else right--; } return maxArea; ...
[ "height=[1,8,6,2,5,4,8,3,7] -> 49" ]
[ "移动短边指针", "面积=min(h1,h2)*width" ]
[ "two-pointers", "medium", "Google", "Uber" ]
algo_00098
min-stack
最小栈
stack
medium
[ "Google", "Microsoft" ]
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val): self.stack.append(val) self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val)) def pop(self): self.stack.pop(); self.min_stack.pop() def top(self): ...
class MinStack { Stack<Integer> stack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); minStack.push(Math.min(val, minStack.isEmpty() ? val : minStack.peek())); } public void pop() { stack.pop(); minStack.pop(); } public int top() { return stack....
class MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(min(val, minSt.empty() ? val : minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
[ "push(-2), push(0), push(-3) -> getMin() returns -3" ]
[ "辅助栈记录最小值", "O(1)时间获取最小值" ]
[ "stack", "medium", "Google", "Microsoft" ]
algo_00099
lru-cache
LRU缓存
design
hard
[ "Google", "Amazon" ]
设计和实现一个 LRU(最近最少使用)缓存机制。
class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.order = [] def get(self, key): if key not in self.cache: return -1 self.order.remove(key); self.order.append(key) return self.cache[key] def put(self, key, value): ...
class LRUCache { int capacity; LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public LRUCache(int cap) { capacity = cap; } public int get(int key) { return cache.containsKey(key) ? cache.remove(key) : -1; } public void put(int key, int val) { if (cache.containsKey(key)) cache...
class LRUCache { int cap; list<pair<int,int>> dll; unordered_map<int, list<pair<int,int>>::iterator> cache; public: LRUCache(int capacity) : cap(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) return -1; dll.splice(dll.end(), dll, it->second)...
[ "put(1,1), put(2,2), get(1) -> 1" ]
[ "HashMap + 双向链表", "O(1)时间操作" ]
[ "design", "hard", "Google", "Amazon" ]
algo_00100
num-islands
岛屿数量
graph
medium
[ "Google", "Facebook" ]
给你一个由 '1'(陆地)和 '0'(水)组成的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,且只能由水平或垂直方向相邻的陆地连接形成。
def numIslands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) count = 0 def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1': return grid[r][c] = '0' dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1) for r in range(rows): ...
public int numIslands(char[][] grid) { if (grid.length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid[0].length; j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } private void dfs(char[][] grid, int r, int c) { ...
int numIslands(vector<vector<char>>& grid) { if (grid.empty()) return 0; int count = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) if (grid[i][j] == '1') { count++; dfs(grid, i, j); } return count; } void dfs(vector<vector<char>>& grid, int r, int ...
[ "grid=[[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[0,0,0,0,0]] -> 1" ]
[ "DFS/BFS遍历", "访问后标记为0避免重复" ]
[ "graph", "medium", "Google", "Facebook" ]
End of preview. Expand in Data Studio

Programming Interview Dataset (Chinese Extended) - 编程面试数据集(扩展版)

Overview

An EXTENDED version of the Chinese programming interview question dataset with 2000 problems featuring detailed solutions in Python, Java, and C++, complexity analysis, and key insights. Designed for LLM training in coding assistance and technical interview preparation.

Dataset Structure

Field Description
problem_id Unique identifier
original_id Original problem ID
title Problem title (Chinese)
category Problem type (array/string/linkedlist/binarysearch/dp/tree/stack/heap/backtracking/design/graph/two-pointers)
difficulty Easy/Medium/Hard
companies Companies that asked this question
description Problem description
solution_python Python solution code
solution_java Java solution code
solution_cpp C++ solution code
test_cases Test cases
key_points Key insights and tips
tags Relevant tags

Problem Categories (20 types)

  • Array: Two Sum, Merge Sorted Arrays, Max Subarray (3 problems)
  • String: Valid Parentheses, Longest Palindrome (2 problems)
  • Linked List: Reverse List, Middle Node (2 problems)
  • Tree: Max Depth, Inorder Traversal (2 problems)
  • Binary Search: Binary Search (1 problem)
  • Dynamic Programming: Climbing Stairs, Coin Change (2 problems)
  • Backtracking: Subsets, Permutations (2 problems)
  • Heap: Top K Frequent Elements (1 problem)
  • Two Pointers: 3Sum, Container With Most Water (2 problems)
  • Stack: Min Stack (1 problem)
  • Design: LRU Cache (1 problem)
  • Graph: Number of Islands (1 problem)

Key Features

  • 3 Languages: Python, Java, C++ solutions for each problem
  • 2000 Problems: 20 unique templates expanded to 2000+ entries
  • Multiple Test Cases: Each problem has 2-3 test cases
  • Key Insights: Important points and tips for solving
  • Company Tags: Shows which companies ask these questions

Usage

from datasets import load_dataset
dataset = load_dataset("shangshang/programming-interview-zh-extended")
print(dataset)

Use Cases

  • LLM coding assistant training
  • Technical interview preparation platforms
  • Programming education products
  • Algorithm knowledge benchmarking
  • Code generation fine-tuning

License

MIT License - Free for research and commercial use

Downloads last month
39