Skip to content

Commit 7bffbaf

Browse files
committed
feat: add cpp solutions to lcof problems No.21,22,24
1 parent 54870da commit 7bffbaf

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

lcof/面试题21. 调整数组顺序使奇数位于偶数前面/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ var exchange = function (nums) {
9393
};
9494
```
9595

96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
vector<int> exchange(vector<int>& nums) {
102+
int left = 0, right = nums.size() - 1;
103+
while (left < right) {
104+
while (left < right && (nums[left] & 1) == 1) {
105+
++left;
106+
}
107+
while (left < right && (nums[right] & 1) == 0) {
108+
--right;
109+
}
110+
swap(nums[left], nums[right]);
111+
}
112+
return nums;
113+
}
114+
};
115+
```
116+
96117
### **...**
97118
98119
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> exchange(vector<int>& nums) {
4+
int left = 0, right = nums.size() - 1;
5+
while (left < right) {
6+
while (left < right && (nums[left] & 1) == 1) {
7+
++left;
8+
}
9+
while (left < right && (nums[right] & 1) == 0) {
10+
--right;
11+
}
12+
swap(nums[left], nums[right]);
13+
}
14+
return nums;
15+
}
16+
};

lcof/面试题22. 链表中倒数第k个节点/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ func getKthFromEnd(head *ListNode, k int) *ListNode {
118118
}
119119
```
120120

121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
ListNode* getKthFromEnd(ListNode* head, int k) {
127+
ListNode *slow = head, *fast = head;
128+
while (k--) {
129+
fast = fast->next;
130+
}
131+
while (fast) {
132+
slow = slow->next;
133+
fast = fast->next;
134+
}
135+
return slow;
136+
}
137+
};
138+
```
139+
121140
### **...**
122141
123142
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
ListNode* getKthFromEnd(ListNode* head, int k) {
4+
ListNode *slow = head, *fast = head;
5+
while (k--) {
6+
fast = fast->next;
7+
}
8+
while (fast) {
9+
slow = slow->next;
10+
fast = fast->next;
11+
}
12+
return slow;
13+
}
14+
};

lcof/面试题24. 反转链表/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,36 @@ func reverseList(head *ListNode) *ListNode {
116116
}
117117
```
118118

119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
ListNode* reverseList(ListNode* head) {
125+
// 通过头插实现逆序
126+
// ListNode *first = new ListNode(-1);
127+
// ListNode *p = head, *q;
128+
// while (p) {
129+
// q = p->next;
130+
// p->next = first->next;
131+
// first->next = p;
132+
// p = q;
133+
// }
134+
// return first->next;
135+
136+
// 常规方法
137+
ListNode *pre = NULL, *cur = head;
138+
while (cur) {
139+
ListNode* temp = cur->next;
140+
cur->next = pre;
141+
pre = cur;
142+
cur = temp;
143+
}
144+
return pre;
145+
}
146+
};
147+
```
148+
119149
### **...**
120150

121151
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
ListNode* reverseList(ListNode* head) {
4+
// 通过头插实现逆序
5+
// ListNode *first = new ListNode(-1);
6+
// ListNode *p = head, *q;
7+
// while (p) {
8+
// q = p->next;
9+
// p->next = first->next;
10+
// first->next = p;
11+
// p = q;
12+
// }
13+
// return first->next;
14+
15+
// 常规方法
16+
ListNode *pre = NULL, *cur = head;
17+
while (cur) {
18+
ListNode* temp = cur->next;
19+
cur->next = pre;
20+
pre = cur;
21+
cur = temp;
22+
}
23+
return pre;
24+
}
25+
};

0 commit comments

Comments
 (0)