-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0065_valid_number.rs
50 lines (42 loc) · 1.25 KB
/
s0065_valid_number.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![allow(unused)]
pub struct Solution {}
impl Solution {
pub fn is_number(s: String) -> bool {
let chs = s.trim().chars().collect::<Vec<char>>();
let (mut point_seen, mut e_seen, mut num_seen, mut num_after_e) =
(false, false, false, true);
for i in 0..chs.len() {
if chs[i] >= '0' && chs[i] <= '9' {
num_seen = true;
num_after_e = true;
} else if chs[i] == '.' {
if e_seen || point_seen {
return false;
}
point_seen = true;
} else if chs[i] == 'e' {
if e_seen || !num_seen {
return false;
}
num_after_e = false;
e_seen = true;
} else if chs[i] == '-' || chs[i] == '+' {
if i != 0 && chs[i - 1] != 'e' {
return false;
}
} else {
return false;
}
}
num_seen && num_after_e
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_65() {
assert_eq!(Solution::is_number("0".to_string()), true);
assert_eq!(Solution::is_number("a".to_string()), false);
}
}