Skip to content

Commit 19d5747

Browse files
committed
feat: add solutions to lcof problem: No.46
面试题46. 把数字翻译成字符串
1 parent e6b2113 commit 19d5747

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,46 @@ public:
125125
};
126126
```
127127
128+
### **TypeScript**
129+
130+
```ts
131+
function translateNum(num: number): number {
132+
let a = 1;
133+
let b = 1;
134+
const str = num + '';
135+
for (let i = 1; i < str.length; i++) {
136+
const val = Number(str[i - 1] + str[i]);
137+
if (val >= 10 && val < 26) {
138+
[a, b] = [b, a + b];
139+
} else {
140+
a = b;
141+
}
142+
}
143+
return b;
144+
}
145+
```
146+
147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn translate_num(num: i32) -> i32 {
152+
let mut a = 1;
153+
let mut b = 1;
154+
let str = num.to_string();
155+
for i in 0..str.len() - 1 {
156+
let c = a + b;
157+
a = b;
158+
let num = str[i..i + 2].parse::<i32>().unwrap();
159+
if num >= 10 && num < 26 {
160+
b = c;
161+
}
162+
}
163+
b
164+
}
165+
}
166+
```
167+
128168
### **...**
129169

130170
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn translate_num(num: i32) -> i32 {
3+
let mut a = 1;
4+
let mut b = 1;
5+
let str = num.to_string();
6+
for i in 0..str.len() - 1 {
7+
let c = a + b;
8+
a = b;
9+
let num = str[i..i + 2].parse::<i32>().unwrap();
10+
if num >= 10 && num < 26 {
11+
b = c;
12+
}
13+
}
14+
b
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function translateNum(num: number): number {
2+
let a = 1;
3+
let b = 1;
4+
const str = num + '';
5+
for (let i = 1; i < str.length; i++) {
6+
const val = Number(str[i - 1] + str[i]);
7+
if (val >= 10 && val < 26) {
8+
[a, b] = [b, a + b];
9+
} else {
10+
a = b;
11+
}
12+
}
13+
return b;
14+
}

0 commit comments

Comments
 (0)