25
25
26
26
<!-- 这里可写通用的实现逻辑 -->
27
27
28
- ~~ ** 最佳方案:** ~~
29
- ~~ 直接返回 ` A * B ` ~~
30
- 正常递归,叠加总和
31
-
32
- ``` txt
33
- MULTIPLY(A, B)
34
- if A == 0 || B == 0
35
- return 0
36
- A + multiply(A, B - 1)
37
- ```
38
-
39
- 优化 1:
40
- 由数值较小的数字决定递归层次
41
-
42
- ``` txt
43
- MULTIPLY(A, B)
44
- if A == 0 || B == 0
45
- return 0
46
- return max(A, B) + multiply(max(A, B), min(A, B) - 1)
47
- ```
48
-
49
- 优化 2:
50
- 使用位移减少递归层次
51
-
52
- ``` txt
53
- MULTIPLY(A, B)
54
- return (B % 1 == 1 ? A : 0) + (B > 1 ? MULTIPLY(A + A, B >> 1) : 0)
55
- ```
28
+ ** 方法一:递归 + 位运算**
56
29
57
- 可进一步,转换为循环,虽然并不符合递归主题 。
30
+ 我们先判断 $B$ 是否为 $1$,如果是,那么直接返回 $A$ 。
58
31
59
- > A 与 B 皆为 ** 正整数 ** ,初始值不会为 0,所以终止条件是 ` B != 1 `
32
+ 否则,我们判断 $B$ 是否为奇数,如果是,那么我们可以将 $B$ 右移一位,然后递归调用函数,最后将结果左移一位,再加上 $A$。否则,我们可以将 $B$ 右移一位,然后递归调用函数,最后将结果左移一位。
60
33
61
- ``` txt
62
- MULTIPLY(A, B)
63
- T = min(A, B)
64
- A = max(A, B)
65
- B = T
66
- r = 0
67
- while B != 1 {
68
- if B % 2 == 1 {
69
- r = r + A
70
- }
71
- A = A + A
72
- B = B >> 1
73
- }
74
- return res + A
75
- ```
34
+ 时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是 $B$ 的大小。
76
35
77
36
<!-- tabs:start -->
78
37
@@ -81,36 +40,75 @@ MULTIPLY(A, B)
81
40
<!-- 这里可写当前语言的特殊实现逻辑 -->
82
41
83
42
``` python
84
-
43
+ class Solution :
44
+ def multiply (self , A : int , B : int ) -> int :
45
+ if B == 1 :
46
+ return A
47
+ if B & 1 :
48
+ return (self .multiply(A, B >> 1 ) << 1 ) + A
49
+ return self .multiply(A, B >> 1 ) << 1
85
50
```
86
51
87
52
### ** Java**
88
53
89
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
90
55
91
56
``` java
92
-
57
+ class Solution {
58
+ public int multiply (int A , int B ) {
59
+ if (B == 1 ) {
60
+ return A ;
61
+ }
62
+ if ((B & 1 ) == 1 ) {
63
+ return (multiply(A , B >> 1 ) << 1 ) + A ;
64
+ }
65
+ return multiply(A , B >> 1 ) << 1 ;
66
+ }
67
+ }
93
68
```
94
69
95
- ### ** TypeScript **
70
+ ### ** C++ **
96
71
97
- ``` ts
98
- function multiply(A : number , B : number ): number {
99
- if (A === 0 || B === 0 ) {
100
- return 0 ;
72
+ ``` cpp
73
+ class Solution {
74
+ public:
75
+ int multiply(int A, int B) {
76
+ if (B == 1) {
77
+ return A;
78
+ }
79
+ if ((B & 1) == 1) {
80
+ return (multiply(A, B >> 1) << 1) + A;
81
+ }
82
+ return multiply(A, B >> 1) << 1;
101
83
}
102
- const [max, min] = [Math .max (A , B ), Math .min (A , B )];
103
- return max + multiply (max , min - 1 );
84
+ };
85
+ ```
86
+
87
+ ### **Go**
88
+
89
+ ```go
90
+ func multiply(A int, B int) int {
91
+ if B == 1 {
92
+ return A
93
+ }
94
+ if B&1 == 1 {
95
+ return (multiply(A, B>>1) << 1) + A
96
+ }
97
+ return multiply(A, B>>1) << 1
104
98
}
105
99
```
106
100
101
+ ### ** TypeScript**
102
+
107
103
``` ts
108
104
function multiply(A : number , B : number ): number {
109
- const max = Math .max (A , B );
110
- const min = Math .min (A , B );
111
- const helper = (a : number , b : number ) =>
112
- (b & 1 ? a : 0 ) + (b > 1 ? helper (a + a , b >> 1 ) : 0 );
113
- return helper (max , min );
105
+ if (B === 1 ) {
106
+ return A ;
107
+ }
108
+ if ((B & 1 ) === 1 ) {
109
+ return (multiply (A , B >> 1 ) << 1 ) + A ;
110
+ }
111
+ return multiply (A , B >> 1 ) << 1 ;
114
112
}
115
113
```
116
114
@@ -119,10 +117,13 @@ function multiply(A: number, B: number): number {
119
117
``` rust
120
118
impl Solution {
121
119
pub fn multiply (a : i32 , b : i32 ) -> i32 {
122
- if a == 0 || b == 0 {
123
- return 0 ;
120
+ if b == 1 {
121
+ return a ;
122
+ }
123
+ if b & 1 == 1 {
124
+ return (Self :: multiply (a , b >> 1 ) << 1 ) + a ;
124
125
}
125
- a . max ( b ) + Self :: multiply (a . max ( b ), a . min ( b ) - 1 )
126
+ Self :: multiply (a , b >> 1 ) << 1
126
127
}
127
128
}
128
129
```
0 commit comments