Skip to content

Commit 651508e

Browse files
committed
feat: add cpp solutions to lcof problems
No. 04,05,06,07,09,11,14,15,16,17
1 parent b91d683 commit 651508e

File tree

21 files changed

+434
-0
lines changed

21 files changed

+434
-0
lines changed

lcof/面试题04. 二维数组中的查找/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,31 @@ func findNumberIn2DArray(matrix [][]int, target int) bool {
123123
}
124124
```
125125

126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
132+
if (matrix.empty()) {
133+
return false;
134+
}
135+
int m = matrix.size(), n = matrix[0].size();
136+
int i = 0, j = n - 1;
137+
while (i < m && j >= 0) {
138+
if (matrix[i][j] == target) {
139+
return true;
140+
} else if (matrix[i][j] < target) {
141+
++i;
142+
} else {
143+
--j;
144+
}
145+
}
146+
return false;
147+
}
148+
};
149+
```
150+
126151
### **...**
127152
128153
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
4+
if (matrix.empty()) {
5+
return false;
6+
}
7+
int m = matrix.size(), n = matrix[0].size();
8+
int i = 0, j = n - 1;
9+
while (i < m && j >= 0) {
10+
if (matrix[i][j] == target) {
11+
return true;
12+
} else if (matrix[i][j] < target) {
13+
++i;
14+
} else {
15+
--j;
16+
}
17+
}
18+
return false;
19+
}
20+
};

lcof/面试题05. 替换空格/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ func replaceSpace(s string) string {
109109
}
110110
```
111111

112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
string replaceSpace(string s) {
118+
string ans;
119+
for (char ch : s) {
120+
if (ch == ' ')
121+
ans += "%20";
122+
else
123+
ans += ch;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
112130
### **...**
113131
114132
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
string replaceSpace(string s) {
4+
string ans;
5+
for (char ch : s) {
6+
if (ch == ' ')
7+
ans += "%20";
8+
else
9+
ans += ch;
10+
}
11+
return ans;
12+
}
13+
};

lcof/面试题06. 从尾到头打印链表/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ func reversePrint(head *ListNode) []int {
124124

125125
### **C++**
126126

127+
- 递归实现
128+
127129
```cpp
128130
/**
129131
* Definition for singly-linked list.
@@ -155,6 +157,28 @@ public:
155157
};
156158
```
157159
160+
- 栈实现
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
vector<int> reversePrint(ListNode* head) {
166+
stack<int> stk;
167+
vector<int> ans;
168+
ListNode *p = head;
169+
while (p) {
170+
stk.push(p->val);
171+
p = p->next;
172+
}
173+
while (!stk.empty()) {
174+
ans.push_back(stk.top());
175+
stk.pop();
176+
}
177+
return ans;
178+
}
179+
};
180+
```
181+
158182
### **...**
159183

160184
```

lcof/面试题07. 重建二叉树/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,33 @@ func helper(preorder, inorder []int, index, start, end int) *TreeNode {
160160
}
161161
```
162162

163+
### **C++**
164+
165+
```cpp
166+
class Solution {
167+
public:
168+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
169+
return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
170+
}
171+
172+
private:
173+
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int pre_l, int pre_r, int in_l, int in_r) {
174+
if (pre_l > pre_r || in_l > in_r) {
175+
return NULL;
176+
}
177+
int root = preorder[pre_l];
178+
int i = in_l;
179+
while (i <= in_r && inorder[i] != root) {
180+
++i;
181+
}
182+
TreeNode* node = new TreeNode(root);
183+
node->left = build(preorder, inorder, pre_l + 1, pre_l + i - in_l, in_l, i - 1);
184+
node->right = build(preorder, inorder, pre_l + i - in_l + 1, pre_r, i + 1, in_r);
185+
return node;
186+
}
187+
};
188+
```
189+
163190
### **...**
164191
165192
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
4+
return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
5+
}
6+
7+
private:
8+
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int pre_l, int pre_r, int in_l, int in_r) {
9+
if (pre_l > pre_r || in_l > in_r) {
10+
return NULL;
11+
}
12+
int root = preorder[pre_l];
13+
int i = in_l;
14+
while (i <= in_r && inorder[i] != root) {
15+
++i;
16+
}
17+
TreeNode* node = new TreeNode(root);
18+
node->left = build(preorder, inorder, pre_l + 1, pre_l + i - in_l, in_l, i - 1);
19+
node->right = build(preorder, inorder, pre_l + i - in_l + 1, pre_r, i + 1, in_r);
20+
return node;
21+
}
22+
};

lcof/面试题09. 用两个栈实现队列/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,38 @@ func (this *CQueue) DeleteHead() int {
174174
}
175175
```
176176

177+
### **C++**
178+
179+
```cpp
180+
class CQueue {
181+
private:
182+
stack<int> s1, s2;
183+
184+
public:
185+
CQueue() {
186+
}
187+
188+
void appendTail(int value) {
189+
s1.push(value);
190+
}
191+
192+
int deleteHead() {
193+
if (s2.empty()) {
194+
while (!s1.empty()) {
195+
s2.push(s1.top());
196+
s1.pop();
197+
}
198+
}
199+
if (s2.empty()) {
200+
return -1;
201+
}
202+
int head = s2.top();
203+
s2.pop();
204+
return head;
205+
}
206+
};
207+
```
208+
177209
### **...**
178210

179211
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class CQueue {
2+
private:
3+
stack<int> s1, s2;
4+
5+
public:
6+
CQueue() {
7+
}
8+
9+
void appendTail(int value) {
10+
s1.push(value);
11+
}
12+
13+
int deleteHead() {
14+
if (s2.empty()) {
15+
while (!s1.empty()) {
16+
s2.push(s1.top());
17+
s1.pop();
18+
}
19+
}
20+
if (s2.empty()) {
21+
return -1;
22+
}
23+
int head = s2.top();
24+
s2.pop();
25+
return head;
26+
}
27+
};

lcof/面试题11. 旋转数组的最小数字/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ func minArray(nums []int) int {
103103
}
104104
```
105105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int minArray(vector<int>& numbers) {
112+
int left = 0, right = numbers.size() - 1;
113+
while (left + 1 < right) {
114+
int mid = left + (right - left) / 2;
115+
if (numbers[mid] > numbers[right]) {
116+
left = mid;
117+
} else if (numbers[mid] < numbers[right]) {
118+
right = mid;
119+
} else {
120+
--right;
121+
}
122+
}
123+
return min(numbers[left], numbers[right]);
124+
}
125+
};
126+
```
127+
106128
### **...**
107129
108130
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int minArray(vector<int>& numbers) {
4+
int left = 0, right = numbers.size() - 1;
5+
while (left + 1 < right) {
6+
int mid = left + (right - left) / 2;
7+
if (numbers[mid] > numbers[right]) {
8+
left = mid;
9+
} else if (numbers[mid] < numbers[right]) {
10+
right = mid;
11+
} else {
12+
--right;
13+
}
14+
}
15+
return min(numbers[left], numbers[right]);
16+
}
17+
};

lcof/面试题14- I. 剪绳子/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ func cuttingRope(n int) int {
108108
}
109109
```
110110

111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int cuttingRope(int n) {
117+
vector<int> dp(n + 1);
118+
dp[0] = 1;
119+
for (int i = 1; i < n; ++i) {
120+
for (int j = i; j <= n; ++j) {
121+
dp[j] = max(dp[j], dp[j - i] * i);
122+
}
123+
}
124+
return dp[n];
125+
}
126+
};
127+
```
128+
111129
### **...**
112130
113131
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int cuttingRope(int n) {
4+
vector<int> dp(n + 1);
5+
dp[0] = 1;
6+
for (int i = 1; i < n; ++i) {
7+
for (int j = i; j <= n; ++j) {
8+
dp[j] = max(dp[j], dp[j - i] * i);
9+
}
10+
}
11+
return dp[n];
12+
}
13+
};

lcof/面试题14- II. 剪绳子 II/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ func cuttingRope(n int) int {
114114
}
115115
```
116116

117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int cuttingRope(int n) {
123+
const int mod = 1000000007;
124+
if (n < 4) return n - 1;
125+
long long ans = 1;
126+
while (n > 4) {
127+
ans = ans * 3 % mod;
128+
n -= 3;
129+
}
130+
if (n == 4)
131+
return ans * 4 % mod;
132+
return ans * n % mod;
133+
}
134+
};
135+
```
136+
117137
### **...**
118138
119139
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int cuttingRope(int n) {
4+
const int mod = 1000000007;
5+
if (n < 4) return n - 1;
6+
long long ans = 1;
7+
while (n > 4) {
8+
ans = ans * 3 % mod;
9+
n -= 3;
10+
}
11+
if (n == 4)
12+
return ans * 4 % mod;
13+
return ans * n % mod;
14+
}
15+
};

0 commit comments

Comments
 (0)