45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
+ ** 方法一:DFS + 数学**
49
+
50
+ 我们设计一个函数 $dfs(i)$,表示从下标 $i$ 开始到最后一个元素的连分数的值,那么答案就是 $dfs(0)$。
51
+
52
+ 函数 $dfs(i)$ 的执行逻辑如下:
53
+
54
+ 如果 $i = n - 1$,只有一个元素,那么它的值就是 $cont[ i] $,分母为 $1$,返回 $[ cont[ i] , 1] $。
55
+
56
+ 否则,我们递归调用 $dfs(i + 1)$,记返回值为 $[ a, b] $,那么 $dfs(i)= 1 + \frac{1}{\frac{a}{b}}$,即 $dfs(i) = \frac{a \times cont[ i] + b}{a}$,分子为 $x = a \times cont[ i] + b$,分母为 $y = a$,我们求出 $x$ 和 $y$ 的最大公约数 $g$,最终返回 $[ \frac{x}{g}, \frac{y}{g}] $。
57
+
58
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $cont$ 的长度。
59
+
48
60
<!-- tabs:start -->
49
61
50
62
### ** Python3**
54
66
``` python
55
67
class Solution :
56
68
def fraction (self , cont : List[int ]) -> List[int ]:
57
- def dfs (cont ):
58
- if len (cont) == 1 :
59
- return [cont[0 ], 1 ]
60
- a, b = dfs(cont[1 :])
61
- return [a * cont[0 ] + b, a]
62
-
63
- return dfs(cont)
69
+ def dfs (i : int ) -> List[int ]:
70
+ if i == len (cont) - 1 :
71
+ return [cont[i], 1 ]
72
+ a, b = dfs(i + 1 )
73
+ x, y = a * cont[i] + b, a
74
+ g = gcd(x, y)
75
+ return [x // g, y // g]
76
+
77
+ return dfs(0 )
64
78
```
65
79
66
80
### ** Java**
@@ -69,17 +83,26 @@ class Solution:
69
83
70
84
``` java
71
85
class Solution {
86
+ private int [] cont;
87
+
72
88
public int [] fraction (int [] cont ) {
73
- return dfs(cont, 0 );
89
+ this . cont = cont;
90
+ return dfs(0 );
74
91
}
75
92
76
- private int [] dfs (int [] cont , int i ) {
93
+ private int [] dfs (int i ) {
77
94
if (i == cont. length - 1 ) {
78
95
return new int [] {cont[i], 1 };
79
96
}
80
- int [] ans = dfs(cont, i + 1 );
81
- int a = ans[0 ], b = ans[1 ];
82
- return new int [] {a * cont[i] + b, a};
97
+ int [] next = dfs(i + 1 );
98
+ int a = next[0 ], b = next[1 ];
99
+ int x = a * cont[i] + b, y = a;
100
+ int g = gcd(x, y);
101
+ return new int [] {x / g, y / g};
102
+ }
103
+
104
+ private int gcd (int a , int b ) {
105
+ return b == 0 ? a : gcd(b, a % b);
83
106
}
84
107
}
85
108
```
@@ -90,14 +113,18 @@ class Solution {
90
113
class Solution {
91
114
public:
92
115
vector<int > fraction(vector<int >& cont) {
93
- return dfs(cont, 0);
94
- }
95
-
96
- vector<int> dfs(vector<int>& cont, int i) {
97
- if (i == cont.size() - 1) return {cont[i], 1};
98
- vector<int> ans = dfs(cont, i + 1);
99
- int a = ans[0], b = ans[1];
100
- return {a * cont[i] + b, a};
116
+ function<vector<int >(int)> dfs = [ &] (int i) {
117
+ if (i == cont.size() - 1) {
118
+ return vector<int >{cont[ i] , 1};
119
+ }
120
+ vector<int > next = dfs(i + 1);
121
+ int a = next[ 0] , b = next[ 1] ;
122
+ int x = a * cont[ i] + b;
123
+ int y = a;
124
+ int g = __ gcd(x, y);
125
+ return vector<int >{x / g, y / g};
126
+ };
127
+ return dfs(0);
101
128
}
102
129
};
103
130
```
@@ -119,6 +146,26 @@ func fraction(cont []int) []int {
119
146
}
120
147
```
121
148
149
+ ### ** TypeScript**
150
+
151
+ ``` ts
152
+ function fraction(cont : number []): number [] {
153
+ const dfs = (i : number ): number [] => {
154
+ if (i === cont .length - 1 ) {
155
+ return [cont [i ], 1 ];
156
+ }
157
+ const [a, b] = dfs (i + 1 );
158
+ const [x, y] = [a * cont [i ] + b , a ];
159
+ const g = gcd (x , y );
160
+ return [x / g , y / g ];
161
+ };
162
+ const gcd = (a : number , b : number ): number => {
163
+ return b === 0 ? a : gcd (b , a % b );
164
+ };
165
+ return dfs (0 );
166
+ }
167
+ ```
168
+
122
169
### ** JavaScript**
123
170
124
171
``` js
@@ -127,13 +174,18 @@ func fraction(cont []int) []int {
127
174
* @return {number[]}
128
175
*/
129
176
var fraction = function (cont ) {
130
- function dfs ( i ) {
177
+ const dfs = i => {
131
178
if (i === cont .length - 1 ) {
132
179
return [cont[i], 1 ];
133
180
}
134
181
const [a , b ] = dfs (i + 1 );
135
- return [a * cont[i] + b, a];
136
- }
182
+ const [x , y ] = [a * cont[i] + b, a];
183
+ const g = gcd (x, y);
184
+ return [Math .floor (x / g), Math .floor (y / g)];
185
+ };
186
+ const gcd = (a , b ) => {
187
+ return b === 0 ? a : gcd (b, a % b);
188
+ };
137
189
return dfs (0 );
138
190
};
139
191
```
0 commit comments