From 80a9cd9e804af4df5ea03fb0c5e3793023ec79bf Mon Sep 17 00:00:00 2001 From: herschel-ma Date: Sun, 17 Dec 2023 14:36:18 +0800 Subject: [PATCH 1/3] feat: add rust solution to lc problem: No.65 --- .../0000-0099/0065.Valid Number/README.md | 65 +++++++++++++++++++ .../0000-0099/0065.Valid Number/README_EN.md | 65 +++++++++++++++++++ .../0000-0099/0065.Valid Number/Solution.rs | 61 +++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 solution/0000-0099/0065.Valid Number/Solution.rs diff --git a/solution/0000-0099/0065.Valid Number/README.md b/solution/0000-0099/0065.Valid Number/README.md index 77174ec8d40ad..97c148df492d0 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** + +``` +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..fb33e3d21774f 100644 --- a/solution/0000-0099/0065.Valid Number/README_EN.md +++ b/solution/0000-0099/0065.Valid Number/README_EN.md @@ -248,6 +248,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/Solution.rs b/solution/0000-0099/0065.Valid Number/Solution.rs new file mode 100644 index 0000000000000..1dc32f692c4e2 --- /dev/null +++ b/solution/0000-0099/0065.Valid Number/Solution.rs @@ -0,0 +1,61 @@ +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 + } +} + From 5409a0a2003bd1ce83893d01f0c289e2556cdc5e Mon Sep 17 00:00:00 2001 From: herschel-ma Date: Sun, 17 Dec 2023 06:38:37 +0000 Subject: [PATCH 2/3] style: format code and docs with prettier --- .../0000-0099/0065.Valid Number/README_EN.md | 11 +- .../0000-0099/0065.Valid Number/Solution.rs | 118 +++++++++--------- 2 files changed, 61 insertions(+), 68 deletions(-) diff --git a/solution/0000-0099/0065.Valid Number/README_EN.md b/solution/0000-0099/0065.Valid Number/README_EN.md index fb33e3d21774f..d1ede25f31a44 100644 --- a/solution/0000-0099/0065.Valid Number/README_EN.md +++ b/solution/0000-0099/0065.Valid Number/README_EN.md @@ -265,13 +265,10 @@ impl Solution { } } 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 - }) + if + x == '.' && + (i + 1 == n || + (if let Some(m) = s.chars().nth(i + 1) { m == 'e' || m == 'E' } else { false })) { return false; } diff --git a/solution/0000-0099/0065.Valid Number/Solution.rs b/solution/0000-0099/0065.Valid Number/Solution.rs index 1dc32f692c4e2..f4dc3fbc62698 100644 --- a/solution/0000-0099/0065.Valid Number/Solution.rs +++ b/solution/0000-0099/0065.Valid Number/Solution.rs @@ -1,61 +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 - } -} - +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 + } +} From 2f46e5f3c9d030acd33e7a966a0620dce1303318 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 17 Dec 2023 15:31:15 +0800 Subject: [PATCH 3/3] Update solution/0000-0099/0065.Valid Number/README.md --- solution/0000-0099/0065.Valid Number/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0065.Valid Number/README.md b/solution/0000-0099/0065.Valid Number/README.md index 97c148df492d0..a702e9d577960 100644 --- a/solution/0000-0099/0065.Valid Number/README.md +++ b/solution/0000-0099/0065.Valid Number/README.md @@ -262,7 +262,7 @@ public class Solution { ### **Rust** -``` +```rust impl Solution { pub fn is_number(s: String) -> bool { let mut i = 0;