Skip to content

Commit

Permalink
0024
Browse files Browse the repository at this point in the history
  • Loading branch information
caixiangyue committed Aug 22, 2023
1 parent f5fbc42 commit 75db6b9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
22 changes: 21 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
"variant": "cpp",
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__std_stream": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"ios": "cpp",
"locale": "cpp",
"queue": "cpp",
"stack": "cpp"
}
}
28 changes: 28 additions & 0 deletions 0024.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "common.h"

namespace s0024 {
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp = cur->next;
ListNode* tmp1 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = tmp;
cur->next->next->next = tmp1;
cur = cur->next->next;
}
return dummyHead->next;
}
};
}
35 changes: 29 additions & 6 deletions test.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
#include "0001.h"
#include "0024.h"
#include <gtest/gtest.h>


TEST(S001, Normal) {
TEST(S0001, Normal) {
s0001::Solution s;
std::vector<int> v = {1,2,3};
// std::vector<int> r = {0,1};
EXPECT_EQ(std::vector<int>({0,1}), s.twoSum(v, 3));
}
std::vector<int> v1 = {1,2,3};
std::vector<int> v2 = {1,1,1};
EXPECT_EQ(std::vector({0,1}), s.twoSum(v1, 3));
EXPECT_EQ(std::vector({0,1}), s.twoSum(v2, 2));
}

s0024::ListNode* createList(vector<int> v) {
s0024::ListNode* head = new s0024::ListNode(v[0]);
s0024::ListNode* tmp = head;
for (int i=1; i<v.size(); i++) {
s0024::ListNode* node = new s0024::ListNode(v[i]);
tmp->next = node;
tmp = tmp->next;
}
return head;
}

TEST(S0024, Normal) {
s0024::Solution s;
s0024::ListNode* t1 = createList(vector<int>{1,2,3,4});
s0024::ListNode* r = s.swapPairs(t1);
vector<int> exp({2,1,4,3});
for (int i=0; i<exp.size(); i++) {
EXPECT_EQ(exp[i], r->val);
r = r->next;
}
}

0 comments on commit 75db6b9

Please sign in to comment.