1
Fork 0
leetcode/source/valid_number/mod.rs

24 lines
556 B
Rust
Raw Normal View History

2022-04-14 11:36:13 +00:00
pub fn is_number(string: String) -> bool {
let string = string.to_lowercase();
if string.contains("inf") || string.contains("nan") {
return false;
}
2022-09-25 09:12:58 +00:00
let mut parts = string.split('e');
2022-04-14 11:36:13 +00:00
let valid_significand = match parts.next() {
Some(part) => part.parse::<f64>().is_ok(),
None => unreachable!(),
};
let valid_exponent = match parts.next() {
Some(part) => part.parse::<isize>().is_ok(),
None => true,
};
let nothing_remaining = parts.next().is_none();
valid_significand && valid_exponent && nothing_remaining
}