11#ifndef LEETCODE_COMMON_H
22#define LEETCODE_COMMON_H
33
4- #include < cassert >
4+ #include < ranges >
55
66namespace listHelper {
77struct 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
6363namespace 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+
6474template <typename T>
6575bool 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}
0 commit comments