LeetCode 题解工作台

笨阶乘

通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。例如, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 。 相反,我们设计了一个笨阶乘 clumsy :在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 栈·状态

bolt

答案摘要

笨阶乘的计算过程可以看作是一个栈的模拟过程。 我们定义一个栈 `stk`,初始时我们将 入栈,定义一个变量 ,表示当前的操作符,初始时 $k = 0$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1

相反,我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。

例如,clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。

另外,我们使用的除法是地板除法(floor division),所以 10 * 9 / 8 等于 11。这保证结果是一个整数。

实现上面定义的笨函数:给定一个整数 N,它返回 N 的笨阶乘。

 

示例 1:

输入:4
输出:7
解释:7 = 4 * 3 / 2 + 1

示例 2:

输入:10
输出:12
解释:12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1

 

提示:

  1. 1 <= N <= 10000
  2. -2^31 <= answer <= 2^31 - 1  (答案保证符合 32 位整数。)
lightbulb

解题思路

方法一:栈 + 模拟

笨阶乘的计算过程可以看作是一个栈的模拟过程。

我们定义一个栈 stk,初始时我们将 nn 入栈,定义一个变量 kk,表示当前的操作符,初始时 k=0k = 0

然后我们从 n1n-1 开始,枚举 xx,根据当前的 kk 的值,决定如何处理 xx

  • k=0k = 0 时,表示乘法操作,我们将栈顶元素出栈,与 xx 相乘后再入栈;
  • k=1k = 1 时,表示除法操作,我们将栈顶元素出栈,与 xx 相除后取整数部分再入栈;
  • k=2k = 2 时,表示加法操作,我们直接将 xx 入栈;
  • k=3k = 3 时,表示减法操作,我们将 x-x 入栈。

接着我们更新 k=(k+1)mod4k = (k + 1) \mod 4

最后,我们将栈中的元素累加即为答案。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为题目给定的整数 NN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def clumsy(self, n: int) -> int:
        k = 0
        stk = [n]
        for x in range(n - 1, 0, -1):
            if k == 0:
                stk.append(stk.pop() * x)
            elif k == 1:
                stk.append(int(stk.pop() / x))
            elif k == 2:
                stk.append(x)
            else:
                stk.append(-x)
            k = (k + 1) % 4
        return sum(stk)
speed

复杂度分析

指标
时间complexity is O(n) because each number from n to 1 is processed once, and each stack operation is O(1). Space complexity is O(n) in the worst case due to storing intermediate results in the stack.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate respects left-to-right multiplication/division precedence before addition/subtraction.

  • question_mark

    Look for efficient stack usage to avoid recalculating intermediate results multiple times.

  • question_mark

    Observe whether the rotation of operations is correctly implemented and cycled through.

warning

常见陷阱

外企场景
  • error

    Failing to apply multiplication and division left to right before addition and subtraction.

  • error

    Incorrectly handling the operation rotation, especially after the first cycle.

  • error

    Attempting recursive factorial computation without considering operation order, leading to wrong results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute clumsy factorial for a subset of numbers with custom operation sequences.

  • arrow_right_alt

    Implement the clumsy factorial using recursion instead of stack simulation.

  • arrow_right_alt

    Optimize space by using variables instead of a stack to track intermediate results.

help

常见问题

外企场景

笨阶乘题解:栈·状态 | LeetCode #1006 中等