Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d4fd65

Browse files
feat: add rust solution to lc problem: No.65 (#2114)
1 parent f0ec4ac commit 5d4fd65

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

‎solution/0000-0099/0065.Valid Number/README.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,71 @@ public class Solution {
260260
}
261261
```
262262

263+
### **Rust**
264+
265+
```rust
266+
impl Solution {
267+
pub fn is_number(s: String) -> bool {
268+
let mut i = 0;
269+
let n = s.len();
270+
271+
if let Some(c) = s.chars().nth(i) {
272+
if c == '+' || c == '-' {
273+
i += 1;
274+
if i == n {
275+
return false;
276+
}
277+
}
278+
}
279+
if let Some(x) = s.chars().nth(i) {
280+
if x == '.'
281+
&& (i + 1 == n
282+
|| if let Some(m) = s.chars().nth(i + 1) {
283+
m == 'e' || m == 'E'
284+
} else {
285+
false
286+
})
287+
{
288+
return false;
289+
}
290+
}
291+
292+
let mut dot = 0;
293+
let mut e = 0;
294+
let mut j = i;
295+
296+
while j < n {
297+
if let Some(c) = s.chars().nth(j) {
298+
if c == '.' {
299+
if e > 0 || dot > 0 {
300+
return false;
301+
}
302+
dot += 1;
303+
} else if c == 'e' || c == 'E' {
304+
if e > 0 || j == i || j == n - 1 {
305+
return false;
306+
}
307+
e += 1;
308+
if let Some(x) = s.chars().nth(j + 1) {
309+
if x == '+' || x == '-' {
310+
j += 1;
311+
if j == n - 1 {
312+
return false;
313+
}
314+
}
315+
}
316+
} else if !c.is_ascii_digit() {
317+
return false;
318+
}
319+
}
320+
j += 1;
321+
}
322+
323+
true
324+
}
325+
}
326+
```
327+
263328
### **...**
264329

265330
```

‎solution/0000-0099/0065.Valid Number/README_EN.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,68 @@ public class Solution {
248248
}
249249
```
250250

251+
### **Rust**
252+
253+
```rust
254+
impl Solution {
255+
pub fn is_number(s: String) -> bool {
256+
let mut i = 0;
257+
let n = s.len();
258+
259+
if let Some(c) = s.chars().nth(i) {
260+
if c == '+' || c == '-' {
261+
i += 1;
262+
if i == n {
263+
return false;
264+
}
265+
}
266+
}
267+
if let Some(x) = s.chars().nth(i) {
268+
if
269+
x == '.' &&
270+
(i + 1 == n ||
271+
(if let Some(m) = s.chars().nth(i + 1) { m == 'e' || m == 'E' } else { false }))
272+
{
273+
return false;
274+
}
275+
}
276+
277+
let mut dot = 0;
278+
let mut e = 0;
279+
let mut j = i;
280+
281+
while j < n {
282+
if let Some(c) = s.chars().nth(j) {
283+
if c == '.' {
284+
if e > 0 || dot > 0 {
285+
return false;
286+
}
287+
dot += 1;
288+
} else if c == 'e' || c == 'E' {
289+
if e > 0 || j == i || j == n - 1 {
290+
return false;
291+
}
292+
e += 1;
293+
if let Some(x) = s.chars().nth(j + 1) {
294+
if x == '+' || x == '-' {
295+
j += 1;
296+
if j == n - 1 {
297+
return false;
298+
}
299+
}
300+
}
301+
} else if !c.is_ascii_digit() {
302+
return false;
303+
}
304+
}
305+
j += 1;
306+
}
307+
308+
true
309+
}
310+
}
311+
```
312+
251313
### **...**
252314

253315
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
impl Solution {
2+
pub fn is_number(s: String) -> bool {
3+
let mut i = 0;
4+
let n = s.len();
5+
6+
if let Some(c) = s.chars().nth(i) {
7+
if c == '+' || c == '-' {
8+
i += 1;
9+
if i == n {
10+
return false;
11+
}
12+
}
13+
}
14+
if let Some(x) = s.chars().nth(i) {
15+
if
16+
x == '.' &&
17+
(i + 1 == n ||
18+
(if let Some(m) = s.chars().nth(i + 1) { m == 'e' || m == 'E' } else { false }))
19+
{
20+
return false;
21+
}
22+
}
23+
24+
let mut dot = 0;
25+
let mut e = 0;
26+
let mut j = i;
27+
28+
while j < n {
29+
if let Some(c) = s.chars().nth(j) {
30+
if c == '.' {
31+
if e > 0 || dot > 0 {
32+
return false;
33+
}
34+
dot += 1;
35+
} else if c == 'e' || c == 'E' {
36+
if e > 0 || j == i || j == n - 1 {
37+
return false;
38+
}
39+
e += 1;
40+
if let Some(x) = s.chars().nth(j + 1) {
41+
if x == '+' || x == '-' {
42+
j += 1;
43+
if j == n - 1 {
44+
return false;
45+
}
46+
}
47+
}
48+
} else if !c.is_ascii_digit() {
49+
return false;
50+
}
51+
}
52+
j += 1;
53+
}
54+
55+
true
56+
}
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /