mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -566,14 +566,24 @@ function restoreIpAddresses(s: string): string[] {
|
|||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn is_valid(s: &Vec<char>, start: usize, end: usize) -> bool {
|
fn is_valid(s: &[char], start: usize, end: usize) -> bool {
|
||||||
if start > end { return false; }
|
if start > end {
|
||||||
if s[start] == '0' && start != end { return false; }
|
return false;
|
||||||
|
}
|
||||||
|
if s[start] == '0' && start != end {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut num = 0;
|
let mut num = 0;
|
||||||
for i in start..=end {
|
for &c in s.iter().take(end + 1).skip(start) {
|
||||||
if s[i] > '9' || s[i] < '0' { return false; }
|
if !('0'..='9').contains(&c) {
|
||||||
if let Some(digit) = s[i].to_digit(10) { num = num * 10 + digit; }
|
return false;
|
||||||
if num > 255 { return false; }
|
}
|
||||||
|
if let Some(digit) = c.to_digit(10) {
|
||||||
|
num = num * 10 + digit;
|
||||||
|
}
|
||||||
|
if num > 255 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user