File tree 3 files changed +45
-3
lines changed
3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change 18
18
19
19
## 解法
20
20
<!-- 这里可写通用的实现逻辑 -->
21
-
21
+ 递归求解。
22
22
23
23
### Python3
24
24
<!-- 这里可写当前语言的特殊实现逻辑 -->
25
25
26
26
``` python
27
-
27
+ class Solution :
28
+ def translateNum (self , num : int ) -> int :
29
+ def cal (s ):
30
+ if len (s) < 2 :
31
+ return 1
32
+ t = int (s[:2 ])
33
+ return cal(s[1 :]) if t < 10 or t > 25 else cal(s[1 :]) + cal(s[2 :])
34
+ return cal(str (num))
28
35
```
29
36
30
37
### Java
31
38
<!-- 这里可写当前语言的特殊实现逻辑 -->
32
39
33
40
``` java
34
-
41
+ class Solution {
42
+ public int translateNum (int num ) {
43
+ return cal(String . valueOf(num));
44
+ }
45
+
46
+ private int cal (String s ) {
47
+ int n = s. length();
48
+ if (n < 2 ) {
49
+ return 1 ;
50
+ }
51
+ int t = Integer . parseInt(s. substring(0 , 2 ));
52
+ return t < 10 || t > 25 ? cal(s. substring(1 )) : cal(s. substring(1 )) + cal(s. substring(2 ));
53
+ }
54
+ }
35
55
```
36
56
37
57
### ...
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int translateNum (int num ) {
3
+ return cal (String .valueOf (num ));
4
+ }
5
+
6
+ private int cal (String s ) {
7
+ int n = s .length ();
8
+ if (n < 2 ) {
9
+ return 1 ;
10
+ }
11
+ int t = Integer .parseInt (s .substring (0 , 2 ));
12
+ return t < 10 || t > 25 ? cal (s .substring (1 )) : cal (s .substring (1 )) + cal (s .substring (2 ));
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def translateNum (self , num : int ) -> int :
3
+ def cal (s ):
4
+ if len (s ) < 2 :
5
+ return 1
6
+ t = int (s [:2 ])
7
+ return cal (s [1 :]) if t < 10 or t > 25 else cal (s [1 :]) + cal (s [2 :])
8
+ return cal (str (num ))
You can’t perform that action at this time.
0 commit comments