Skip to content

Commit

Permalink
leetcode: Add cpp impl to 0468
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 24, 2024
1 parent 06a0764 commit e820fdb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/leetcode/0468.validate-ip-address/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@
```rust
{{#include src/main.rs:5:82}}
```

## C++

```cpp
{{#include main.cpp:5:100}}
```
27 changes: 19 additions & 8 deletions src/leetcode/0468.validate-ip-address/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <sstream>
#include <string>
#include <sstream>

class Solution {
public:
Expand All @@ -15,14 +16,13 @@ class Solution {
// 并判断每个部分是有效的数值
// 数值不带有前缀0

if (query[0] == '.' || query[query.size() - 1] == '.') {
if (query.empty() || query[0] == '.' || query[query.size() - 1] == '.') {
return false;
}
int part_count = 0;
std::stringstream ss(query);
std::string part;
int part_count = 0;
while (std::getline(ss, part, '.')) {
part_count += 1;
// 数值不带有前缀0
if (part[0] == '0' && part.size() > 1) {
return false;
Expand All @@ -37,15 +37,19 @@ class Solution {
}
}

const int val = std::stoi(part);
size_t pos = 0;
const int val = std::stoi(part, &pos);
// 不是有效的整数
if (val == 0 && part != "0") {
return false;
if (pos != part.size()) {
//return false;
}

// 数值范围是 0..255
if (val < 0 || val > 255) {
return false;
}

part_count += 1;
}

// 要有4个部分
Expand All @@ -58,13 +62,13 @@ class Solution {
// 可以有0作为前缀
// 不需要考虑缩写

if (query[0] == ':' || query[query.size() - 1] == ':') {
if (query.empty() || query[0] == ':' || query[query.size() - 1] == ':') {
return false;
}

int part_count = 0;
std::stringstream ss(query);
std::string part;
int part_count = 0;
while (std::getline(ss, part, ':')) {
// 1-4个字符
if (part.size() < 1 || part.size() > 4) {
Expand All @@ -77,6 +81,7 @@ class Solution {
return false;
}
}

part_count += 1;
}

Expand All @@ -95,6 +100,12 @@ class Solution {
};

void checkSolution() {
{
const std::string s1 = "172.16.254.1";
const std::string expected = "IPv4";
const std::string out = Solution::validIPAddress(s1);
assert(out == expected);
}
{
const std::string s1 = "2001:0db8:85a3:0:0:8A2E:0370:7334:";
const std::string expected = "Neither";
Expand Down
6 changes: 3 additions & 3 deletions src/leetcode/0468.validate-ip-address/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn is_ipv4(query: &str) -> bool {
}

for part in parts {
if part.len() < 1 || part.len() > 3 {
if part.is_empty() || part.len() > 3 {
return false;
}

Expand All @@ -31,7 +31,7 @@ fn is_ipv4(query: &str) -> bool {

if let Ok(val) = part.parse::<i32>() {
// 数值范围是 0..255
if val < 0 || val > 255 {
if !(0..=255).contains(&val) {
return false;
}
} else {
Expand All @@ -56,7 +56,7 @@ fn is_ipv6(query: &str) -> bool {

for part in parts {
// 1-4个字符
if part.len() < 1 || part.len() > 4 {
if part.is_empty() || part.len() > 4 {
return false;
}

Expand Down

0 comments on commit e820fdb

Please sign in to comment.