Skip to content

Commit 57cb9ac

Browse files
committed
feat: add solutions to lc problem: No.1694
No.1694.Reformat Phone Number
1 parent 565e47f commit 57cb9ac

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

solution/1600-1699/1694.Reformat Phone Number/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,46 @@ func reformatNumber(number string) string {
198198
}
199199
```
200200

201+
### **TypeScript**
202+
203+
```ts
204+
function reformatNumber(number: string): string {
205+
const cs = [...number].filter(c => c !== ' ' && c !== '-');
206+
const n = cs.length;
207+
return cs
208+
.map((v, i) => {
209+
if (
210+
((i + 1) % 3 === 0 && i < n - 2) ||
211+
(n % 3 === 1 && n - 3 === i)
212+
) {
213+
return v + '-';
214+
}
215+
return v;
216+
})
217+
.join('');
218+
}
219+
```
220+
221+
### **Rust**
222+
223+
```rust
224+
impl Solution {
225+
pub fn reformat_number(number: String) -> String {
226+
let cs: Vec<char> = number.chars().filter(|&c| c != ' ' && c != '-').collect();
227+
let n = cs.len();
228+
cs.iter()
229+
.enumerate()
230+
.map(|(i, c)| {
231+
if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
232+
return c.to_string() + &"-";
233+
}
234+
c.to_string()
235+
})
236+
.collect()
237+
}
238+
}
239+
```
240+
201241
### **...**
202242

203243
```

solution/1600-1699/1694.Reformat Phone Number/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,46 @@ func reformatNumber(number string) string {
159159
}
160160
```
161161

162+
### **TypeScript**
163+
164+
```ts
165+
function reformatNumber(number: string): string {
166+
const cs = [...number].filter(c => c !== ' ' && c !== '-');
167+
const n = cs.length;
168+
return cs
169+
.map((v, i) => {
170+
if (
171+
((i + 1) % 3 === 0 && i < n - 2) ||
172+
(n % 3 === 1 && n - 3 === i)
173+
) {
174+
return v + '-';
175+
}
176+
return v;
177+
})
178+
.join('');
179+
}
180+
```
181+
182+
### **Rust**
183+
184+
```rust
185+
impl Solution {
186+
pub fn reformat_number(number: String) -> String {
187+
let cs: Vec<char> = number.chars().filter(|&c| c != ' ' && c != '-').collect();
188+
let n = cs.len();
189+
cs.iter()
190+
.enumerate()
191+
.map(|(i, c)| {
192+
if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
193+
return c.to_string() + &"-";
194+
}
195+
c.to_string()
196+
})
197+
.collect()
198+
}
199+
}
200+
```
201+
162202
### **...**
163203

164204
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn reformat_number(number: String) -> String {
3+
let cs: Vec<char> = number.chars().filter(|&c| c != ' ' && c != '-').collect();
4+
let n = cs.len();
5+
cs.iter()
6+
.enumerate()
7+
.map(|(i, c)| {
8+
if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
9+
return c.to_string() + &"-";
10+
}
11+
c.to_string()
12+
})
13+
.collect()
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function reformatNumber(number: string): string {
2+
const cs = [...number].filter(c => c !== ' ' && c !== '-');
3+
const n = cs.length;
4+
return cs
5+
.map((v, i) => {
6+
if (
7+
((i + 1) % 3 === 0 && i < n - 2) ||
8+
(n % 3 === 1 && n - 3 === i)
9+
) {
10+
return v + '-';
11+
}
12+
return v;
13+
})
14+
.join('');
15+
}

0 commit comments

Comments
 (0)