LeetCode 题解工作台
最小偶倍数
给你一个正整数 n ,返回 2 和 n 的最小公倍数(正整数)。 示例 1: 输入: n = 5 输出: 10 解释: 5 和 2 的最小公倍数是 10 。 示例 2: 输入: n = 6 输出: 6 解释: 6 和 2 的最小公倍数是 6 。注意数字会是它自身的倍数。 提示: 1
2
题型
7
代码语言
3
相关题
当前训练重点
简单 · 数学·结合·number·theory
答案摘要
如果 为偶数,那么 和 的最小公倍数就是 本身。否则, 和 的最小公倍数就是 $n \times 2$。 时间复杂度 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数学·结合·number·theory 题型思路
题目描述
n ,返回 2 和 n 的最小公倍数(正整数)。
示例 1:
输入:n = 5 输出:10 解释:5 和 2 的最小公倍数是 10 。
示例 2:
输入:n = 6 输出:6 解释:6 和 2 的最小公倍数是 6 。注意数字会是它自身的倍数。
提示:
1 <= n <= 150
解题思路
方法一:数学
如果 为偶数,那么 和 的最小公倍数就是 本身。否则, 和 的最小公倍数就是 。
时间复杂度 。
class Solution:
def smallestEvenMultiple(self, n: int) -> int:
return n if n % 2 == 0 else n * 2
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(1) because the solution only requires a single parity check and potentially one multiplication. Space complexity is O(1) as no extra storage is needed beyond the input and output. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Asks if n is even or odd to guide towards a simple formula.
- question_mark
Looks for a constant time solution without loops.
- question_mark
Wants understanding of minimal number theory application for multiples.
常见陷阱
外企场景- error
Forgetting that even numbers return n itself, leading to unnecessary multiplication.
- error
Overcomplicating by trying to find least common multiple manually.
- error
Not considering constraints, assuming n could be very large and optimizing incorrectly.
进阶变体
外企场景- arrow_right_alt
Find smallest multiple divisible by 3 and n instead of 2.
- arrow_right_alt
Compute smallest multiple divisible by a given k and n.
- arrow_right_alt
Generalize to find smallest multiple divisible by multiple given numbers.