Skip to content

Commit 8fefb81

Browse files
committed
一刷1006
1 parent 505898a commit 8fefb81

File tree

6 files changed

+115
-35
lines changed

6 files changed

+115
-35
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7067,14 +7067,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
70677067
//|{doc_base_url}/1005-maximize-sum-of-array-after-k-negations.adoc[题解]
70687068
//|Easy
70697069
//|
7070-
//
7071-
//|{counter:codes}
7072-
//|{leetcode_base_url}/clumsy-factorial/[1006. Clumsy Factorial^]
7073-
//|{source_base_url}/_1006_ClumsyFactorial.java[Java]
7074-
//|{doc_base_url}/1006-clumsy-factorial.adoc[题解]
7075-
//|Medium
7076-
//|
7077-
//
7070+
7071+
|{counter:codes}
7072+
|{leetcode_base_url}/clumsy-factorial/[1006. Clumsy Factorial^]
7073+
|{source_base_url}/_1006_ClumsyFactorial.java[Java]
7074+
|{doc_base_url}/1006-clumsy-factorial.adoc[题解]
7075+
|Medium
7076+
|
7077+
70787078
//|{counter:codes}
70797079
//|{leetcode_base_url}/minimum-domino-rotations-for-equal-row/[1007. Minimum Domino Rotations For Equal Row^]
70807080
//|{source_base_url}/_1007_MinimumDominoRotationsForEqualRow.java[Java]

docs/1006-clumsy-factorial.adoc

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,72 @@
11
[#1006-clumsy-factorial]
2-
= 1006. Clumsy Factorial
2+
= 1006. 笨阶乘
33

4-
{leetcode}/problems/clumsy-factorial/[LeetCode - Clumsy Factorial^]
4+
https://leetcode.cn/problems/clumsy-factorial/[LeetCode - 1006. 笨阶乘 ^]
55

6-
Normally, the factorial of a positive integer `n` is the product of all positive integers less than or equal to `n`. For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`.
6+
通常,正整数 `n` 的阶乘是所有小于或等于 `n` 的正整数的乘积。例如,`factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`
77

8-
We instead make a _clumsy factorial:_ using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.
8+
相反,我们设计了一个笨阶乘 `clumsy`:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)
99
10-
For example, `clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1`. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
10+
例如,`clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1`。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。
1111
12-
Additionally, the division that we use is _floor division_ such that `10 * 9 / 8` equals `11`. This guarantees the result is an integer.
12+
另外,我们使用的除法是地板除法(_floor division_),所以 `10 * 9 / 8` 等于 `11`。这保证结果是一个整数。
1313
14-
`<font face="sans-serif, Arial, Verdana, Trebuchet MS">Implement the clumsy` function as defined above: given an integer `N`, it returns the clumsy factorial of `N`.
14+
实现上面定义的笨函数:给定一个整数 `N`,它返回 `N` 的笨阶乘。
1515
16-
1716
18-
*Example 1:*
17+
*示例 1:*
1918

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* 4
23-
*Output:* 7
24-
*Explanation:* 7 = 4 * 3 / 2 + 1
25-
----
19+
....
20+
输入:4
21+
输出:7
22+
解释:7 = 4 * 3 / 2 + 1
23+
....
2624

27-
*Example 2:*
25+
*示例 2:*
2826

29-
[subs="verbatim,quotes,macros"]
30-
----
31-
*Input:* 10
32-
*Output:* 12
33-
*Explanation:* 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
34-
----
27+
....
28+
输入:10
29+
输出:12
30+
解释:12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
31+
....
3532

36-
3733

38-
*Note:*
34+
*提示:*
3935

36+
. `+1 <= N <= 10000+`
37+
. `-2^31^ \<= answer \<= 2^31^ - 1` (答案保证符合 32 位整数。)
4038

41-
. `1 <= N <= 10000`
42-
. `-2^31 <= answer <= 2^31 - 1` (The answer is guaranteed to fit within a 32-bit integer.)
4339

40+
== 思路分析
4441

42+
看官方题解,可以把减法改成“加负数”,这样就可以极大地减少判断,可以直接按照“遇到乘除立即算,遇到加减先入栈”处理。
4543

44+
image::images/1006-10.gif[{image_attr}]
4645

4746
[[src-1006]]
47+
[tabs]
48+
====
49+
一刷::
50+
+
51+
--
4852
[{java_src_attr}]
4953
----
5054
include::{sourcedir}/_1006_ClumsyFactorial.java[tag=answer]
5155
----
56+
--
57+
58+
// 二刷::
59+
// +
60+
// --
61+
// [{java_src_attr}]
62+
// ----
63+
// include::{sourcedir}/_1006_ClumsyFactorial_2.java[tag=answer]
64+
// ----
65+
// --
66+
====
67+
68+
69+
== 参考资料
5270

71+
. https://leetcode.cn/problems/clumsy-factorial/solutions/692629/ben-jie-cheng-by-leetcode-solution-deh2/[1006. 笨阶乘 - 官方题解^] -- 可以把减法改成“加负数”,这样就可以极大地减少判断。
72+
. https://leetcode.cn/problems/clumsy-factorial/solutions/693117/fu-xue-ming-zhu-yu-dao-cheng-chu-li-ji-s-furg/[1006. 笨阶乘 - 【负雪明烛】遇到乘除立即算,遇到加减先入栈^]

docs/images/1006-10.gif

4.3 MB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ include::0994-rotting-oranges.adoc[leveloffset=+1]
20952095

20962096
// include::1005-maximize-sum-of-array-after-k-negations.adoc[leveloffset=+1]
20972097

2098-
// include::1006-clumsy-factorial.adoc[leveloffset=+1]
2098+
include::1006-clumsy-factorial.adoc[leveloffset=+1]
20992099

21002100
// include::1007-minimum-domino-rotations-for-equal-row.adoc[leveloffset=+1]
21012101

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,11 @@ endif::[]
798798
|{doc_base_url}/1217-minimum-cost-to-move-chips-to-the-same-position.adoc[题解]
799799
|❌ 题目理解错误!`position[i]` 中的数字是筹码的位置。题目意思是奇数到奇数和偶数到偶数免费,但是紧挨的两个数字移动收费,所以,统计奇偶数的个数,看哪个小就移动哪个。
800800

801+
|{counter:codes2503}
802+
|{leetcode_base_url}/clumsy-factorial/[1006. 笨阶乘^]
803+
|{doc_base_url}/1006-clumsy-factorial.adoc[题解]
804+
|✅ 栈。代码写出来了,但是好烂!
805+
801806
|===
802807

803808
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.function.BiFunction;
6+
7+
public class _1006_ClumsyFactorial {
8+
// tag::answer[]
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-05-23 14:54:02
12+
*/
13+
public int clumsy(int n) {
14+
char[] operators = new char[]{'*', '/', '+', '-'};
15+
BiFunction<Integer, Integer, Integer>[] ops = new BiFunction[4];
16+
ops[0] = (x, y) -> x * y;
17+
ops[1] = (x, y) -> x / y;
18+
ops[2] = Integer::sum;
19+
ops[3] = (x, y) -> x - y;
20+
int idx = 0;
21+
int result = 0;
22+
Deque<Integer> stack = new ArrayDeque<>(n);
23+
stack.offer(n--);
24+
while (!stack.isEmpty() && n > 0) {
25+
if (idx == 2 && stack.size() == 2) {
26+
Integer b = stack.pollLast();
27+
Integer a = stack.pollLast();
28+
stack.offer(ops[3].apply(a, b));
29+
continue;
30+
}
31+
if (idx == 0 || idx == 1 || idx == 2) {
32+
Integer pre = stack.pollLast();
33+
Integer res = ops[idx].apply(pre, n--);
34+
stack.offerLast(res);
35+
} else {
36+
stack.offerLast(n--);
37+
}
38+
idx++;
39+
idx %= operators.length;
40+
}
41+
if (stack.size() == 1) {
42+
result = stack.pollLast();
43+
} else {
44+
Integer b = stack.pollLast();
45+
Integer a = stack.pollLast();
46+
result = ops[3].apply(a, b);
47+
}
48+
return result;
49+
}
50+
// end::answer[]
51+
52+
public static void main(String[] args) {
53+
new _1006_ClumsyFactorial().clumsy(4);
54+
}
55+
}

0 commit comments

Comments
 (0)