Skip to content

Commit 7135bdc

Browse files
author
kwu130
committed
feat(src): add 93-restoreIpAddresses and enhance common.h
1 parent 61bf23a commit 7135bdc

2 files changed

Lines changed: 84 additions & 12 deletions

File tree

‎include/common.h‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef LEETCODE_COMMON_H
22
#define LEETCODE_COMMON_H
33

4-
#include <cassert>
4+
#include <ranges>
55

66
namespace listHelper {
77
struct ListNode {
@@ -10,17 +10,17 @@ struct ListNode {
1010
ListNode()
1111
: val(0),
1212
next(nullptr) {}
13-
ListNode(int x)
13+
explicit ListNode(int x)
1414
: val(x),
1515
next(nullptr) {}
16-
ListNode(int x, ListNode* next)
16+
ListNode(const int x, ListNode* next)
1717
: val(x),
1818
next(next) {}
1919
};
2020

21-
ListNode* build(const std::vector<int>& vec) {
21+
inline ListNode* build(const std::vector<int>& vec) {
2222
if (vec.empty()) return nullptr;
23-
ListNode* head = new ListNode(vec[0]);
23+
auto* head = new ListNode(vec[0]);
2424
ListNode* cur = head;
2525
for (size_t i = 1; i < vec.size(); i++) {
2626
cur->next = new ListNode(vec[i]);
@@ -30,7 +30,7 @@ ListNode* build(const std::vector<int>& vec) {
3030
return head;
3131
}
3232

33-
bool equal(const ListNode* lhs, const ListNode* rhs) {
33+
inline bool equal(const ListNode* lhs, const ListNode* rhs) {
3434
while (lhs != nullptr && rhs != nullptr) {
3535
if (lhs->val != rhs->val) {
3636
return false;
@@ -42,15 +42,15 @@ bool equal(const ListNode* lhs, const ListNode* rhs) {
4242
return lhs == nullptr && rhs == nullptr;
4343
}
4444

45-
void print(const ListNode* head) {
45+
inline void print(const ListNode* head) {
4646
while (head != nullptr) {
4747
std::cout << head->val << " -> ";
4848
head = head->next;
4949
}
5050
std::cout << "nullptr" << std::endl;
5151
}
5252

53-
void free(ListNode* head) {
53+
inline void free(ListNode* head) {
5454
while (head != nullptr) {
5555
ListNode* tmp = head;
5656
head = head->next;
@@ -61,15 +61,25 @@ void free(ListNode* head) {
6161

6262

6363
namespace compareHelper {
64+
template <typename T>
65+
bool equalIgnoreOrder(std::vector<T> a, std::vector<T> b) {
66+
if (a.size() != b.size()) return false;
67+
68+
std::ranges::sort(a);
69+
std::ranges::sort(b);
70+
71+
return a == b;
72+
}
73+
6474
template <typename T>
6575
bool equalIgnoreOrder(std::vector<std::vector<T>> a, std::vector<std::vector<T>> b) {
6676
if (a.size() != b.size()) return false;
6777

68-
for (auto& v : a) std::sort(v.begin(), v.end());
69-
for (auto& v : b) std::sort(v.begin(), v.end());
78+
for (auto& v : a) std::ranges::sort(v);
79+
for (auto& v : b) std::ranges::sort(v);
7080

71-
std::sort(a.begin(), a.end());
72-
std::sort(b.begin(), b.end());
81+
std::ranges::sort(a);
82+
std::ranges::sort(b);
7383

7484
return a == b;
7585
}

‎src/93-restoreIpAddresses.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Created by 1412 on 2025/11/23.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "common.h"
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
vector<string> restoreIpAddresses(string s) {
13+
vector<string> res;
14+
dfs(s, 0, {}, res);
15+
return res;
16+
}
17+
18+
private:
19+
void dfs(const string s, int index, vector<string>&& path, vector<string>& res) {
20+
if (path.size() == 4) {
21+
if (index == static_cast<int>(s.size())) {
22+
res.emplace_back(path[0] + "." + path[1] + "." + path[2] + "." + path[3]);
23+
}
24+
return;
25+
}
26+
for (int l = 1; l <= 3; ++l) {
27+
if (index + l > static_cast<int>(s.size())) {
28+
return;
29+
}
30+
if (l != 1 && s[index] == '0') {
31+
return;
32+
}
33+
string segment = s.substr(index, l);
34+
if (l == 3 && stoi(segment) > 255) {
35+
return;
36+
}
37+
path.push_back(segment);
38+
dfs(s, index + l, std::move(path), res);
39+
path.pop_back();
40+
}
41+
}
42+
};
43+
44+
void test() {
45+
Solution s;
46+
vector<string> expected = {"255.255.11.135", "255.255.111.35"};
47+
vector<string> res = s.restoreIpAddresses("25525511135");
48+
assert(compareHelper::equalIgnoreOrder(res, expected));
49+
50+
expected = {"0.0.0.0"};
51+
res = s.restoreIpAddresses("0000");
52+
assert(compareHelper::equalIgnoreOrder(res, expected));
53+
54+
expected = {"1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"};
55+
res = s.restoreIpAddresses("101023");
56+
assert(compareHelper::equalIgnoreOrder(res, expected));
57+
}
58+
59+
int main() {
60+
test();
61+
return 0;
62+
}

0 commit comments

Comments
 (0)