Skip to content

Commit c9038d0

Browse files
committed
feat: add python and java solutions to lcof question
添加《剑指 Offer》题解:面试题46. 把数字翻译成字符串
1 parent b17b2f4 commit c9038d0

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

lcof/面试题46. 把数字翻译成字符串/README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,40 @@
1818

1919
## 解法
2020
<!-- 这里可写通用的实现逻辑 -->
21-
21+
递归求解。
2222

2323
### Python3
2424
<!-- 这里可写当前语言的特殊实现逻辑 -->
2525

2626
```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))
2835
```
2936

3037
### Java
3138
<!-- 这里可写当前语言的特殊实现逻辑 -->
3239

3340
```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+
}
3555
```
3656

3757
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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))

0 commit comments

Comments
 (0)