Skip to content

Commit 0fa25ae

Browse files
author
xwk1246
committed
first commit
0 parents  commit 0fa25ae

File tree

20 files changed

+574
-0
lines changed

20 files changed

+574
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!*.*

1.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
#include <unordered_set>
4+
#include <cmath>
5+
using namespace std;
6+
7+
vector<int> twoSum(vector<int>& nums, int target) {
8+
unordered_map<int, int>record;
9+
for (int i = 0; i < nums.size(); i++) {
10+
unordered_map<int, int>::iterator result;
11+
if ((result = record.find(target - nums[i])) != record.end()) {
12+
return { i, result->second };
13+
}
14+
record.insert(pair<int, int>(nums[i], i));
15+
}
16+
return {};
17+
}

142.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
3+
ListNode* detectCycle(ListNode* head) {
4+
ListNode* slow = head;
5+
ListNode* fast = head;
6+
while (fast->next && fast->next->next) {
7+
fast = fast->next->next;
8+
slow = slow->next;
9+
if (fast == slow) {
10+
slow = head;
11+
while (fast != slow) {
12+
fast = fast->next;
13+
slow = slow->next;
14+
}
15+
return fast;
16+
}
17+
}
18+
return nullptr;
19+
}

160.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct ListNode {
5+
int val;
6+
ListNode* next;
7+
ListNode() {}
8+
ListNode(int x) :val(x), next(nullptr) {}
9+
ListNode(int x, ListNode* next) :val(x), next(next) {}
10+
};
11+
12+
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
13+
int sizeA = 0, sizeB = 0;
14+
ListNode* ptrA = new ListNode();
15+
ptrA->next = headA;
16+
ListNode* ptrB = new ListNode();
17+
ptrB->next = headB;
18+
ListNode* tmp = ptrA;
19+
while (tmp->next) {
20+
sizeA++;
21+
tmp = tmp->next;
22+
}
23+
tmp = ptrB;
24+
while (tmp->next) {
25+
sizeB++;
26+
tmp = tmp->next;
27+
}
28+
if (sizeA > sizeB) {
29+
for (int i = 0; i < sizeA - sizeB; i++) {
30+
ptrA = ptrA->next;
31+
}
32+
}
33+
else if (sizeB > sizeA) {
34+
for (int i = 0; i < sizeB - sizeA; i++) {
35+
ptrB = ptrB->next;
36+
}
37+
}
38+
while (ptrA && ptrB) {
39+
if (ptrA == ptrB)return ptrA;
40+
ptrA = ptrA->next;
41+
ptrB = ptrB->next;
42+
}
43+
return nullptr;
44+
}
45+
46+
int main() {
47+
ListNode* tmp;
48+
ListNode* headA = new ListNode(0, tmp = new ListNode(1, new ListNode(2)));
49+
ListNode* headB = new ListNode(5, new ListNode(4, tmp));
50+
cout << getIntersectionNode(headA, headB);
51+
}

19.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct ListNode {
5+
int val;
6+
ListNode* next;
7+
ListNode() :val(0), next(nullptr) {}
8+
ListNode(int x) :val(x), next(nullptr) {}
9+
ListNode(int x, ListNode* next) :val(x), next(next) {}
10+
};
11+
12+
ListNode* removeNthFromEnd(ListNode* head, int n) {
13+
ListNode* dummy = new ListNode(0, head);
14+
ListNode* fast = dummy;
15+
ListNode* slow = dummy;
16+
for (int i = 0; i < n + 1; i++) {
17+
fast = fast->next;
18+
}
19+
while (fast) {
20+
fast = fast->next;
21+
slow = slow->next;
22+
}
23+
ListNode* tmp = slow->next;
24+
slow->next = slow->next->next;
25+
delete tmp;
26+
return dummy->next;
27+
}
28+
29+
int main() {
30+
ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3)));
31+
cout << removeNthFromEnd(head, 3)->val;
32+
}

202.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_set>
4+
using namespace std;
5+
6+
bool isHappy(int n) {
7+
unordered_set<int>record;
8+
int tmp = n;
9+
int sum = 0;
10+
while (sum != 1) {
11+
sum = 0;
12+
while (tmp) {
13+
sum += (tmp % 10) * (tmp % 10);
14+
tmp /= 10;
15+
}
16+
tmp = sum;
17+
if (record.find(sum) != record.end())return false;
18+
record.insert(sum);
19+
}
20+
return true;
21+
}
22+
23+
int main() {
24+
cout << isHappy(2);
25+
return 0;
26+
}

203.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
struct ListNode {
6+
int val;
7+
ListNode* next;
8+
ListNode() : val(0), next(nullptr) {}
9+
ListNode(int x) : val(x), next(nullptr) {}
10+
ListNode(int x, ListNode* next) : val(x), next(next) {}
11+
};
12+
13+
ListNode* removeElements(ListNode* head, int val) {
14+
ListNode* tmp;
15+
ListNode* dummy = new ListNode(0, head);
16+
ListNode* current = dummy;
17+
while (current->next) {
18+
if (current->next->val == val) {
19+
tmp = current->next;
20+
current->next = current->next->next;
21+
delete tmp;
22+
}
23+
else {
24+
current = current->next;
25+
}
26+
}
27+
return dummy->next;
28+
}
29+
30+
31+
int main() {
32+
ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3, nullptr)));
33+
removeElements(head, 2);
34+
return 0;
35+
}

206.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
ListNode* reverse(ListNode last, ListNode cur) {
6+
if (cur)return last;
7+
ListNode* tmp;
8+
tmp = cur->next;
9+
cur->next = last;
10+
return reverse(cur, tmp);
11+
}
12+
13+
ListNode* reverseList(ListNode* head) {
14+
reverse(nullptr, cent nodes and return its head.You must solve the problem without modifying the values in the list's nodehead)
15+
}
16+
17+
int main() {
18+
return 0;
19+
}

209.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
int minSubArrayLen(int target, vector<int>& nums) {
6+
int min = 0;
7+
int sum, tmp;
8+
int i = 0;
9+
int j = 0;
10+
int k;
11+
while (i < nums.size() && j < nums.size()) {
12+
sum = 0;
13+
for (k = i; k <= j; k++) {
14+
sum += nums[k];
15+
}
16+
if (sum >= target && (j - i + 1 < min || min == 0))min = j - i + 1;
17+
if (sum >= target) {
18+
i++;
19+
}
20+
else if (sum < target) {
21+
j++;
22+
}
23+
}
24+
return min;
25+
}
26+
int main() {
27+
vector <int> max = { 2,3,1,2,4,3 };
28+
int target = 7;
29+
cout << minSubArrayLen(target, max);
30+
}

24.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct ListNode {
5+
int val;
6+
ListNode* next;
7+
ListNode() :val(0), next(nullptr) {}
8+
ListNode(int x) :val(x), next(nullptr) {}
9+
ListNode(int x, ListNode* next) :val(x), next(next) {}
10+
};
11+
12+
ListNode* swapPairs(ListNode* head) {
13+
ListNode* dummy = new ListNode(0, head);
14+
ListNode* cur = dummy;
15+
ListNode* tmp;
16+
while (cur->next && cur->next->next) {
17+
tmp = cur->next->next;
18+
cur->next->next = cur->next->next->next;
19+
tmp->next = cur->next;
20+
cur->next = tmp;
21+
cur = cur->next->next;
22+
}
23+
return dummy->next;
24+
}
25+
26+
int main() {
27+
ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
28+
ListNode* swapped = swapPairs(head);
29+
return 0;
30+
}

0 commit comments

Comments
 (0)