diff --git a/solution/0000-0099/0065.Valid Number/README.md b/solution/0000-0099/0065.Valid Number/README.md index 77174ec8d40ad..a702e9d577960 100644 --- a/solution/0000-0099/0065.Valid Number/README.md +++ b/solution/0000-0099/0065.Valid Number/README.md @@ -260,6 +260,71 @@ public class Solution { } ``` +### **Rust** + +```rust +impl Solution { + pub fn is_number(s: String) -> bool { + let mut i = 0; + let n = s.len(); + + if let Some(c) = s.chars().nth(i) { + if c == '+' || c == '-' { + i += 1; + if i == n { + return false; + } + } + } + if let Some(x) = s.chars().nth(i) { + if x == '.' + && (i + 1 == n + || if let Some(m) = s.chars().nth(i + 1) { + m == 'e' || m == 'E' + } else { + false + }) + { + return false; + } + } + + let mut dot = 0; + let mut e = 0; + let mut j = i; + + while j < n { + if let Some(c) = s.chars().nth(j) { + if c == '.' { + if e > 0 || dot > 0 { + return false; + } + dot += 1; + } else if c == 'e' || c == 'E' { + if e > 0 || j == i || j == n - 1 { + return false; + } + e += 1; + if let Some(x) = s.chars().nth(j + 1) { + if x == '+' || x == '-' { + j += 1; + if j == n - 1 { + return false; + } + } + } + } else if !c.is_ascii_digit() { + return false; + } + } + j += 1; + } + + true + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0065.Valid Number/README_EN.md b/solution/0000-0099/0065.Valid Number/README_EN.md index b95129c241a19..d1ede25f31a44 100644 --- a/solution/0000-0099/0065.Valid Number/README_EN.md +++ b/solution/0000-0099/0065.Valid Number/README_EN.md @@ -248,6 +248,68 @@ public class Solution { } ``` +### **Rust** + +```rust +impl Solution { + pub fn is_number(s: String) -> bool { + let mut i = 0; + let n = s.len(); + + if let Some(c) = s.chars().nth(i) { + if c == '+' || c == '-' { + i += 1; + if i == n { + return false; + } + } + } + if let Some(x) = s.chars().nth(i) { + if + x == '.' && + (i + 1 == n || + (if let Some(m) = s.chars().nth(i + 1) { m == 'e' || m == 'E' } else { false })) + { + return false; + } + } + + let mut dot = 0; + let mut e = 0; + let mut j = i; + + while j < n { + if let Some(c) = s.chars().nth(j) { + if c == '.' { + if e > 0 || dot > 0 { + return false; + } + dot += 1; + } else if c == 'e' || c == 'E' { + if e > 0 || j == i || j == n - 1 { + return false; + } + e += 1; + if let Some(x) = s.chars().nth(j + 1) { + if x == '+' || x == '-' { + j += 1; + if j == n - 1 { + return false; + } + } + } + } else if !c.is_ascii_digit() { + return false; + } + } + j += 1; + } + + true + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0065.Valid Number/Solution.rs b/solution/0000-0099/0065.Valid Number/Solution.rs new file mode 100644 index 0000000000000..f4dc3fbc62698 --- /dev/null +++ b/solution/0000-0099/0065.Valid Number/Solution.rs @@ -0,0 +1,57 @@ +impl Solution { + pub fn is_number(s: String) -> bool { + let mut i = 0; + let n = s.len(); + + if let Some(c) = s.chars().nth(i) { + if c == '+' || c == '-' { + i += 1; + if i == n { + return false; + } + } + } + if let Some(x) = s.chars().nth(i) { + if + x == '.' && + (i + 1 == n || + (if let Some(m) = s.chars().nth(i + 1) { m == 'e' || m == 'E' } else { false })) + { + return false; + } + } + + let mut dot = 0; + let mut e = 0; + let mut j = i; + + while j < n { + if let Some(c) = s.chars().nth(j) { + if c == '.' { + if e > 0 || dot > 0 { + return false; + } + dot += 1; + } else if c == 'e' || c == 'E' { + if e > 0 || j == i || j == n - 1 { + return false; + } + e += 1; + if let Some(x) = s.chars().nth(j + 1) { + if x == '+' || x == '-' { + j += 1; + if j == n - 1 { + return false; + } + } + } + } else if !c.is_ascii_digit() { + return false; + } + } + j += 1; + } + + true + } +}