LeetCode 题解工作台

最小偶倍数

给你一个正整数 n ,返回 2 和 n 的最小公倍数(正整数)。 示例 1: 输入: n = 5 输出: 10 解释: 5 和 2 的最小公倍数是 10 。 示例 2: 输入: n = 6 输出: 6 解释: 6 和 2 的最小公倍数是 6 。注意数字会是它自身的倍数。 提示: 1

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·结合·number·theory

bolt

答案摘要

如果 为偶数,那么 和 的最小公倍数就是 本身。否则, 和 的最小公倍数就是 $n \times 2$。 时间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·结合·number·theory 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个正整数 n ,返回 2 n 的最小公倍数(正整数)。

 

示例 1:

输入:n = 5
输出:10
解释:5 和 2 的最小公倍数是 10 。

示例 2:

输入:n = 6
输出:6
解释:6 和 2 的最小公倍数是 6 。注意数字会是它自身的倍数。

 

提示:

  • 1 <= n <= 150
lightbulb

解题思路

方法一:数学

如果 nn 为偶数,那么 22nn 的最小公倍数就是 nn 本身。否则,22nn 的最小公倍数就是 n×2n \times 2

时间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def smallestEvenMultiple(self, n: int) -> int:
        return n if n % 2 == 0 else n * 2
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

最小偶倍数题解:数学·结合·number·theory | LeetCode #2413 简单