File tree 3 files changed +184
-0
lines changed
solution/0000-0099/0065.Valid Number
3 files changed +184
-0
lines changed Original file line number Diff line number Diff line change @@ -260,6 +260,71 @@ public class Solution {
260
260
}
261
261
```
262
262
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
+
263
328
### ** ...**
264
329
265
330
```
Original file line number Diff line number Diff line change @@ -248,6 +248,68 @@ public class Solution {
248
248
}
249
249
```
250
250
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
+
251
313
### ** ...**
252
314
253
315
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments